Try getting the LastInsertID before the rows affected

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
Andrew Thornton 2023-03-14 16:48:54 +00:00
parent 5a188e58d1
commit b4467b717a
No known key found for this signature in database
GPG Key ID: 3CDE74631F13A748
1 changed files with 6 additions and 6 deletions

View File

@ -212,6 +212,8 @@ func (session *Session) upsertStruct(doUpdate bool, bean interface{}) (int64, er
session.incrVersionFieldValue(verValue) session.incrVersionFieldValue(verValue)
} }
} }
id, iderr := res.LastInsertId()
n, err := res.RowsAffected() n, err := res.RowsAffected()
if err != nil || n == 0 { if err != nil || n == 0 {
return 0, err return 0, err
@ -222,14 +224,12 @@ func (session *Session) upsertStruct(doUpdate bool, bean interface{}) (int64, er
n = 1 n = 1
} }
if table.AutoIncrement == "" { if iderr != nil || id <= 0 {
return n, nil return n, err
} }
var id int64 if table.AutoIncrement == "" {
id, err = res.LastInsertId() return n, nil
if err != nil || id <= 0 {
return n, err
} }
aiValue, err := table.AutoIncrColumn().ValueOf(bean) aiValue, err := table.AutoIncrColumn().ValueOf(bean)