保存所有字段
Save 会保存所有的字段,即使字段是零值
db.First(&user)user.Name = "jinzhu 2"user.Age = 100db.Save(&user)// UPDATE users SET name='jinzhu 2', age=100, birthday='2016-01-01', updated_at = '2013-11-17 21:34:10' WHERE id=111;
Update/Updates
使用 Update、Updates 可以更新选定的字段
// 更新单个字段// the user of `Model(&user)` needs to have primary key value, it is `111` in this exampledb.Model(&user).Update("name", "hello")// UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE id=111;// 根据条件更新单个字段db.Model(&user).Where("active = ?", true).Update("name", "hello")// UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE id=111 AND active=true;// 通过 `struct` 更新多个字段,不会更新零值字段db.Model(&user).Updates(User{Name: "hello", Age: 18, Active: false})// UPDATE users SET name='hello', age=18, updated_at = '2013-11-17 21:34:10' WHERE id = 111;// 通过 `map` 更新多个字段,零值字段也会更新db.Model(&user).Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})// UPDATE users SET name='hello', age=18, actived=false, updated_at='2013-11-17 21:34:10' WHERE id=111;
注意 当通过 struct 更新时,GORM 只会更新非零字段。 如果您想确保指定字段被更新,你应该使用 Select 更新选定字段,或使用 map 来完成更新操作
更新选定字段
如果您想要在更新时选定、忽略某些字段,您可以使用 Select、Omit
// Select 与 Map// the user of `Model(&user)` needs to have primary key value, it is `111` in this exampledb.Model(&user).Select("name").Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})// UPDATE users SET name='hello' WHERE id=111;db.Model(&user).Omit("name").Updates(map[string]interface{}{"name": "hello", "age": 18, "actived": false})// UPDATE users SET age=18, actived=false, updated_at='2013-11-17 21:34:10' WHERE id=111;// Select 与 StructDB.Model(&result).Select("Name", "Age").Updates(User{Name: "new_name"})// UPDATE users SET name='new_name', age=0 WHERE id=111;
更新钩子
对于更新操作,GORM 支持 BeforeSave、BeforeUpdate、AfterSave、AfterUpdate 钩子,这些方法将在更新记录时被调用,详情请参阅 钩子
func (u *User) BeforeUpdate(tx *gorm.DB) (err error) {if u.Role == "admin" {return errors.New("admin user not allowed to update")}return}
批量更新
如果您尚未通过 Model 指定记录的主键,则 GORM 会执行批量更新
// 通过 struct 只能更新非零值,若要更新零值,可以使用 map[string]interface{}db.Model(User{}).Where("role = ?", "admin").Updates(User{Name: "hello", Age: 18})// UPDATE users SET name='hello', age=18 WHERE role = 'admin;db.Table("users").Where("id IN (?)", []int{10, 11}).Updates(map[string]interface{}{"name": "hello", "age": 18})// UPDATE users SET name='hello', age=18 WHERE id IN (10, 11);
阻止全局更新
如果在没有任何条件的情况下执行批量更新,GORM 不会执行该操作,并返回ErrMissingWhereClause错误
您可以使用 1 = 1 之类的条件来强制全局更新
db.Model(&User{}).Update("name", "jinzhu").Error // gorm.ErrMissingWhereClausedb.Model(&User{}).Where("1 = 1").Update("name", "jinzhu")// UPDATE users SET `name` = "jinzhu" WHERE 1=1
更新的记录数
// 通过 `RowsAffected` 得到更新的记录数result := db.Model(User{}).Where("role = ?", "admin").Updates(User{Name: "hello", Age: 18})// UPDATE users SET name='hello', age=18 WHERE role = 'admin;result.RowsAffected // 更新的记录数result.Error // 更新的错误
高级用法
通过 SQL 表达式更新
GORM 允许通过 SQL 表达式更新列
DB.Model(&product).Update("price", gorm.Expr("price * ? + ?", 2, 100))// UPDATE "products" SET "price" = price * '2' + '100', "updated_at" = '2013-11-17 21:34:10' WHERE "id" = '2';DB.Model(&product).Updates(map[string]interface{}{"price": gorm.Expr("price * ? + ?", 2, 100)})// UPDATE "products" SET "price" = price * '2' + '100', "updated_at" = '2013-11-17 21:34:10' WHERE "id" = '2';DB.Model(&product).UpdateColumn("quantity", gorm.Expr("quantity - ?", 1))// UPDATE "products" SET "quantity" = quantity - 1 WHERE "id" = '2';DB.Model(&product).Where("quantity > 1").UpdateColumn("quantity", gorm.Expr("quantity - ?", 1))// UPDATE "products" SET "quantity" = quantity - 1 WHERE "id" = '2' AND quantity > 1;
不使用钩子和时间追踪
如果您想在更新时跳过 钩子 方法和自动更新时间追踪, 您可以使用 UpdateColumn、UpdateColumns
// 更新单列,用法类似于 `Update`db.Model(&user).UpdateColumn("name", "hello")// UPDATE users SET name='hello' WHERE id = 111;// 更新多列,用法类似于 `Updates`db.Model(&user).UpdateColumns(User{Name: "hello", Age: 18})// UPDATE users SET name='hello', age=18 WHERE id = 111;// 配合 Select 更新多列,用法类似于 `Updates`db.Model(&user).Select("name", "age").UpdateColumns(User{Name: "hello"})// UPDATE users SET name='hello', age=0 WHERE id = 111;
检查字段是否有变更?
GORM 提供的 Changed 方法可以在 Before 钩子中检查字段是否有变更
Changed 方法只能与 Update、Updates 方法一起使用,它只是检查 Model 对象字段的值与 Update、Updates 的值是否相等,以及该字段是否会被更新(例如,可以通过 Select、Omit 排除某些字段),如果不相等,则返回 true,并更新记录
func (u *User) BeforeUpdate(tx *gorm.DB) (err error) {// 如果 role 字段有变更if tx.Statement.Changed("Role") {return errors.New("role not allowed to change")}if tx.Statement.Changed("Name", "Admin") { // 如果 Name 或 Role 字段有变更tx.Statement.SetColumn("Age", 18)}// 如果任意字段有变更if tx.Statement.Changed() {tx.Statement.SetColumn("RefreshedAt", time.Now())}return nil}db.Model(&User{ID: 1, Name: "jinzhu"}).Updates(map[string]interface{"name": "jinzhu2"})// Changed("Name") => truedb.Model(&User{ID: 1, Name: "jinzhu"}).Updates(map[string]interface{"name": "jinzhu"})// Changed("Name") => false, 因为 `Name` 没有变更db.Model(&User{ID: 1, Name: "jinzhu"}).Select("Admin").Updates(map[string]interface{"name": "jinzhu2", "admin": false,})// Changed("Name") => false, 因为 `Name` 没有被 Select 选中并更新db.Model(&User{ID: 1, Name: "jinzhu"}).Updates(User{Name: "jinzhu2"})// Changed("Name") => truedb.Model(&User{ID: 1, Name: "jinzhu"}).Updates(User{Name: "jinzhu"})// Changed("Name") => false, 因为 `Name` 没有变更db.Model(&User{ID: 1, Name: "jinzhu"}).Select("Admin").Updates(User{Name: "jinzhu2"})// Changed("Name") => false, 因为 `Name` 没有被 Select 选中并更新
在更新时修改值
若要在 Before 钩子中改变要更新的值,如果它是一个完整的更新,可以使用 Save;否则,应该使用 scope.SetColumn ,例如:
func (user *User) BeforeSave(scope *gorm.Scope) (err error) {if pw, err := bcrypt.GenerateFromPassword(user.Password, 0); err == nil {scope.SetColumn("EncryptedPassword", pw)}}db.Model(&user).Update("Name", "jinzhu")