我需要更新多行中字段的值。
我正在查询以获取一些数据库行,但它不起作用。
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
关于go - 更新 "jinzhu/gorm"pkg 中的行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61267010/