Problem with Record in transaction
ThanksSirAlex opened this issue · 4 comments
ThanksSirAlex commented
func (srv *Service) createHistory(tx *dbr.Tx) (*model.SomeStruct, error) {
someStruct := model.SomeStruct{
UserID: srv.UserId,
}
_, err := tx.InsertInto("xxx").
Columns("user_id"").
Record(&lendingHistory).
Exec()
fmt.Println(lendingHistory.ID)
return someStruct, nil
}
I have some code like this. But Record
doesn't set the ID automaticly. I Still Get 0.
version in go.mod
is github.com/gocraft/dbr v0.0.0-20190626032649-cb950044475e
taylorchu commented
- what db driver are you using?
- what is the struct like for lendingHistory?
thanks!
ThanksSirAlex commented
- I'm using Mysql. Connection is wrote like
var mysqlConn *dbr.Connection
func MysqlConn() *dbr.Connection {
return mysqlConn
}
func NewMySQLSession() *dbr.Session {
return mysqlConn.NewSession(nil)
}
2 lendingHistroy is a table in our business. I'm sure it has a ID
it's like
type LendingHistory struct {
ID int `db:"id"`
UserID int `db:"user_id"`
Days int `db:"days"`
EndTime int64 `db:"end_time"`
Description string `db:"description"`
}
I'm not sure if this is cause by using transaction. I've tried to prient ID
after commit but still got 0
taylorchu commented
The type of ID needs to be int64. The type of LastInsertID from go driver is int64, so if we support int
, some value cannot be represented and causes a problem.
ThanksSirAlex commented
@taylorchu thank you, that works. BTW, I think you should put it in the doc, I thought it could be any int type according to your doc.