无法更新 "jinzhu/gorm" pkg 中的行

Failed to update rows in "jinzhu/gorm" pkg

我需要更新多行字段的值。

我正在查询以获取一些数据库行,但它不起作用。

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