fix: 'Duplicate entry' while Save() composite primary key model

This commit is contained in:
Y!an 2023-11-24 01:28:03 +08:00
parent 3207ad6033
commit a6f869a340
No known key found for this signature in database
GPG Key ID: 9FC6B4064FA5A809

View File

@ -89,7 +89,11 @@ func (db *DB) Save(value interface{}) (tx *DB) {
if err := tx.Statement.Parse(value); err == nil && tx.Statement.Schema != nil { if err := tx.Statement.Parse(value); err == nil && tx.Statement.Schema != nil {
for _, pf := range tx.Statement.Schema.PrimaryFields { for _, pf := range tx.Statement.Schema.PrimaryFields {
if _, isZero := pf.ValueOf(tx.Statement.Context, reflectValue); isZero { if _, isZero := pf.ValueOf(tx.Statement.Context, reflectValue); isZero {
return tx.callbacks.Create().Execute(tx) if _, ok := tx.Statement.Clauses["ON CONFLICT"]; !ok {
tx = tx.Clauses(clause.OnConflict{UpdateAll: true})
}
return tx.callbacks.Create().Execute(tx.Set("gorm:update_track_time", true))
} }
} }
} }