GORM Playground Link

https://github.com/go-gorm/playground/pull/166

Description

Per documentation, Save should insert a record only if the value passed doesn't have a PK. So if it has a PK, it shouldn't create it.

Comment From: jinzhu

Hi,

V2's behaviour works as expected, if you want to update a record, you can use Updates, thank you.

Comment From: PrashantRaj18198

Please update the docs to reflect this.

In the docs, this piece of code is present in the "Update" section which says

db.First(&user)

user.Name = "jinzhu 2"
user.Age = 100
db.Save(&user)
// UPDATE users SET name='jinzhu 2', age=100, birthday='2016-01-01', updated_at = '2013-11-17 21:34:10' WHERE id=111;

Docs reference link: https://gorm.io/docs/update.html#Save-All-Fields

Comment From: alingse

I have the same problem I found it was because call update data twice (or nothing change from the db), the updateTx can get tx.Error == nil && tx.RowsAffected == 0

https://github.com/go-gorm/gorm/blob/master/finisher_api.go#L107-L109

updateTx := tx.callbacks.Update().Execute(tx.Session(&Session{Initialized: true}))
if updateTx.Error == nil && updateTx.RowsAffected == 0 && !updateTx.DryRun && !selectedUpdate {
    return tx.Create(value)
}