我需要更新行的值。
我有一个查询来获取一些数据库行。但这不起作用。
DB.Where("is_send = ?", "0").Find(&artists)
for _, artist := range artists {
if condition {
artist.IsSend = 1
... (more updatee)
DB.Save(&artist)
}
}
通过参考以下示例来更改范围:
for _, elem := range elems {
elem = new_val // Won't work, because elem is a copy of
// the value from elems
}
for i := range elems {
elems[i] = new_val // Works, because elems[i] deferences
// the pointer to the actual value in elems
}
读取:Gotchas
此外,如果您不修改所有字段,而不是使用Save
,则也可以使用Update
。参考:GORM CRUD's Interface UPDATE