mirror of https://github.com/go-gorm/gorm.git
parent
d2dd0ce4a7
commit
0c7e575f19
|
@ -106,7 +106,7 @@ func (db *DB) Save(value interface{}) (tx *DB) {
|
|||
updateTx := tx.callbacks.Update().Execute(tx.Session(&Session{Initialized: true}))
|
||||
|
||||
if updateTx.Error == nil && updateTx.RowsAffected == 0 && !updateTx.DryRun && !selectedUpdate {
|
||||
return tx.Create(value)
|
||||
return tx.Clauses(clause.OnConflict{UpdateAll: true}).Create(value)
|
||||
}
|
||||
|
||||
return updateTx
|
||||
|
|
|
@ -610,6 +610,25 @@ func TestUpdateFromSubQuery(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIdempotentSave(t *testing.T) {
|
||||
create := Company{
|
||||
Name: "company_idempotent",
|
||||
}
|
||||
DB.Create(&create)
|
||||
|
||||
var company Company
|
||||
if err := DB.Find(&company, "id = ?", create.ID).Error; err != nil {
|
||||
t.Fatalf("failed to find created company, got err: %v", err)
|
||||
}
|
||||
|
||||
if err := DB.Save(&company).Error; err != nil || company.ID != create.ID {
|
||||
t.Errorf("failed to save company, got err: %v", err)
|
||||
}
|
||||
if err := DB.Save(&company).Error; err != nil || company.ID != create.ID {
|
||||
t.Errorf("failed to save company, got err: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSave(t *testing.T) {
|
||||
user := *GetUser("save", Config{})
|
||||
DB.Create(&user)
|
||||
|
|
Loading…
Reference in New Issue