Postgresql 我想忽略的更新覆盖字段的Gorm模型
我将gorm(GoLang)与PostgreSQL一起使用。 最近,为了节省时间和最佳实践,我决定使用与gorm的关联 所以问题是我想更新一个模型而不覆盖他的所有字段 例如“我有这个结构:Postgresql 我想忽略的更新覆盖字段的Gorm模型,postgresql,go,associations,go-gorm,Postgresql,Go,Associations,Go Gorm,我将gorm(GoLang)与PostgreSQL一起使用。 最近,为了节省时间和最佳实践,我决定使用与gorm的关联 所以问题是我想更新一个模型而不覆盖他的所有字段 例如“我有这个结构: type Car struct { gorm.Model CarModels []CarModel `json:"car_models"` Foo string `json:"foo"` } type CarModel struct { gorm.Mode
type Car struct {
gorm.Model
CarModels []CarModel `json:"car_models"`
Foo string `json:"foo"`
}
type CarModel struct {
gorm.Model
Name string `json:"name"`
Bar string `json:"-"`
CarID uint `json:"car_id"`
}
- 用户只能看到车型名称和车号。
- 用户更改了车型名称并要求保存
- 我想保存对DB和Gorm的更改
DB.Update()
- 所以我保存了汽车模型(不是CarModel!!!),因为它们是关联的
有人知道如何处理这个问题吗?如何更新
汽车
对象?完整的db.Update()
命令是什么?`sqlConnection,err:=o.Connect(),如果err!=nil{return}defer sqlConnection.Close()sqlConnection.Save(&myCar)sqlConnection.Close()return`正是问题所在。您应该使用db.Update()
而不是db.Save()
。检查:Save
将在执行更新SQL时包括所有字段,即使它没有更改..但我们需要将字段传递给db.Update(),我有一百万个字段。。。我看到他们有db.Updates(),并且要将整个结构作为param传递//使用struct
更新多个属性,将只更新那些已更改的非空字段db.Model(&user).Updates(user{Name:“hello”,Age:18})///更新users SET Name='hello',Age=18,updated_at='2013-11-17 21:34:10',其中id=111;当然,然后使用db.Updates()
而不是db.Update()
。如何更新Car
对象?完整的db.Update()
命令是什么?`sqlConnection,err:=o.Connect(),如果err!=nil{return}defer sqlConnection.Close()sqlConnection.Save(&myCar)sqlConnection.Close()return`正是问题所在。您应该使用db.Update()
而不是db.Save()
。检查:Save
将在执行更新SQL时包括所有字段,即使它没有更改..但我们需要将字段传递给db.Update(),我有一百万个字段。。。我看到他们有db.Updates(),并且要将整个结构作为param传递//使用struct
更新多个属性,将只更新那些已更改的非空字段db.Model(&user).Updates(user{Name:“hello”,Age:18})///更新users SET Name='hello',Age=18,updated_at='2013-11-17 21:34:10',其中id=111;当然,然后使用db.Updates()
而不是db.Update()
。