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