diff --git a/callback_create.go b/callback_create.go index 87aba8ee..fd5fea11 100644 --- a/callback_create.go +++ b/callback_create.go @@ -128,6 +128,8 @@ func createCallback(scope *Scope) { if result, err := scope.SQLDB().Exec(scope.SQL, scope.SQLVars...); scope.Err(err) == nil { // set rows affected count scope.db.RowsAffected, _ = result.RowsAffected() + // set lastInsertId + scope.db.LastInsertId, _ = result.LastInsertId() // set primary value to primary field if primaryField != nil && primaryField.IsBlank { diff --git a/main.go b/main.go index 67e5f58e..d604b117 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ type DB struct { Value interface{} Error error RowsAffected int64 + LastInsertId int64 // single db db SQLCommon diff --git a/scope.go b/scope.go index 541fe522..7ada09a1 100644 --- a/scope.go +++ b/scope.go @@ -382,6 +382,9 @@ func (scope *Scope) Exec() *Scope { if count, err := result.RowsAffected(); scope.Err(err) == nil { scope.db.RowsAffected = count } + if lastInsertId, err := result.LastInsertId(); scope.Err(err) == nil { + scope.db.LastInsertId = lastInsertId + } } } return scope