Gorm: Model().Save() does not generate WHERE clause

Created on 24 Sep 2020  路  4Comments  路  Source: go-gorm/gorm

GORM Playground Link

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

Description

value := &Test{Value: "foo"}
db.Model(Test{}).Create(&value)
value.Value = "bar"
db.Model(Test{}).Save(&value)
--> Save: WHERE condition required

Bug, or no longer supported to use Model?

db.Save(&value) --> OK
with reproduction steps

Most helpful comment

@jinzhu The condition is generated without the .Model(),but you don't require a .Where() before a .Save() so I don't understand how this isn't a bug.

All 4 comments

@jinzhu The condition is generated without the .Model(),but you don't require a .Where() before a .Save() so I don't understand how this isn't a bug.

@jinzhu

    db.Debug().Model(Test{}).Save(&test)
Outputs
[0.284ms] [rows:0] UPDATE `tests` SET `id`=1,`value`="test"

    db.Debug().Save(&test)
Outputs
[0.525ms] [rows:0] UPDATE `tests` SET `value`="test" WHERE `id` = 1

Having same problem, wonder if there is a way to do gorm v1-like Save, which is an upsert function.

Was this page helpful?
0 / 5 - 0 ratings