refactor if branch

This commit is contained in:
方圣卿 2023-10-27 17:05:52 +08:00
parent 5b4695a334
commit bccc8fdf1e

View File

@ -103,9 +103,15 @@ func Create(config *Config) func(db *gorm.DB) {
} }
db.RowsAffected, _ = result.RowsAffected() db.RowsAffected, _ = result.RowsAffected()
if db.RowsAffected != 0 && db.Statement.Schema != nil && if db.RowsAffected == 0 {
db.Statement.Schema.PrioritizedPrimaryField != nil && return
db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue { }
if db.Statement.Schema != nil {
if db.Statement.Schema.PrioritizedPrimaryField == nil || !db.Statement.Schema.PrioritizedPrimaryField.HasDefaultValue {
return
}
insertID, err := result.LastInsertId() insertID, err := result.LastInsertId()
insertOk := err == nil && insertID > 0 insertOk := err == nil && insertID > 0
if !insertOk { if !insertOk {
@ -147,11 +153,9 @@ func Create(config *Config) func(db *gorm.DB) {
db.AddError(db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, db.Statement.ReflectValue, insertID)) db.AddError(db.Statement.Schema.PrioritizedPrimaryField.Set(db.Statement.Context, db.Statement.ReflectValue, insertID))
} }
} }
} } else if db.Statement.Dest != nil {
// append @id column with value for auto-increment primary key
// append @id column with value for auto-increment primary key // the @id value is correct, when: 1. without setting auto-increment primary key, 2. database AutoIncrementIncrement = 1
// the @id value is correct, when: 1. without setting auto-increment primary key, 2. database AutoIncrementIncrement = 1
if db.RowsAffected != 0 && db.Statement.Schema == nil && db.Statement.Dest != nil {
insertID, err := result.LastInsertId() insertID, err := result.LastInsertId()
insertOk := err == nil && insertID > 0 insertOk := err == nil && insertID > 0
if !insertOk { if !insertOk {