GORM—改

简介: GORM—改

更新所有字段

如果主键是零值,更新所有记录

  var user User
  user.Age = 33
  db.Debug().Save(&user)
[2022-05-12 21:21:09]  [2.00ms]  INSERT INTO `users` (`age`) VALUES (33)  
[1 rows affected or returned ] 

如果主键不是零值,则更新该条记录

  var user User
  user.Age = 33
  user.ID = 2
  db.Debug().Save(&user)
[2022-05-12 21:21:52]  [1.75ms]  UPDATE `users` SET `name` = NULL, `age` = 33  WHERE `users`.`id` = 2  
[1 rows affected or returned ] 

指定字段更新

如果你只希望更新指定字段,可以使用Update或者Updates

  var user User
  db.First(&user)
  // 更新单个属性,如果它有变化
  // UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE id=1;
  db.Model(&user).Update("name", "hello")
  // 根据给定的条件更新单个属性
  // UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE id=1 AND active=true;
  db.Model(&user).Where("active = ?", true).Update("name", "hello")
  // 使用 map 更新多个属性,只会更新其中有变化的属性
  // UPDATE users SET name='hello', age=18, active=false, updated_at='2013-11-17 21:34:10' WHERE id=1;
  db.Model(&user).Updates(map[string]interface{}{"name": "hello", "age": 18, "active": false})
  // 使用 struct 更新多个属性,只会更新其中有变化且为非零值的字段
  // UPDATE users SET name='hello', age=18, updated_at = '2013-11-17 21:34:10' WHERE id = 1;
  db.Model(&user).Updates(User{Name: "hello", Age: 18})
  // 警告:当使用 struct 更新时,GORM只会更新那些非零值的字段
  // 对于下面的操作,不会发生任何更新,"", 0, false 都是其类型的零值
  db.Model(&user).Updates(User{Name: "", Age: 0, Active: false})

更新选定字段

如果你想更新或忽略某些字段,你可以使用 Select,Omit

  //只更新name字段
  // UPDATE users SET name='hello', updated_at='2013-11-17 21:34:10' WHERE id=111;
  db.Model(&user).Select("name").Updates(map[string]interface{}{"name": "hello", "age": 18, "active": false})
  //忽略name字段
  // UPDATE users SET age=18, active=false, updated_at='2013-11-17 21:34:10' WHERE id=111;
  db.Model(&user).Omit("name").Updates(map[string]interface{}{"name": "hello", "age": 18, "active": false})

无Hooks更新

上面的更新操作会自动运行 model 的 BeforeUpdate, AfterUpdate 方法,更新 UpdatedAt 时间戳, 在更新时保存其 Associations, 如果你不想调用这些方法,你可以使用 UpdateColumn,

UpdateColumns

  // 更新单个属性,类似于 `Update`
  // UPDATE users SET name='hello' WHERE id = 1;
  db.Model(&user).UpdateColumn("name", "hello")
  // 更新多个属性,类似于 `Updates`
  // UPDATE users SET name='hello', age=18 WHERE id = 1;
  db.Model(&user).UpdateColumns(User{Name: "hello", Age: 18})

批量更新

批量更新时Hooks(钩子函数)不会运行。

  // UPDATE users SET name='hello', age=18 WHERE id IN (10, 11);
  db.Table("users").Where("id IN (?)", []int{10, 11}).Updates(map[string]interface{}{"name": "hello", "age": 18})
  // 使用 struct 更新时,只会更新非零值字段,若想更新所有字段,请使用map[string]interface{}
  // UPDATE users SET name='hello', age=18;
  db.Model(User{}).Updates(User{Name: "hello", Age: 18})
  // 使用 `RowsAffected` 获取更新记录总数
  affected := db.Model(User{}).Updates(User{Name: "hello", Age: 18}).RowsAffected

使用SQL表达式更新

先查询表中的第一条数据保存至user变量。

  // UPDATE `users` SET `age` = age * 2 + 100, `updated_at` = '2020-02-16 13:10:20'  WHERE `users`.`id` = 1;
  db.Model(&user).Update("age", gorm.Expr("age * ? + ?", 2, 100))
  // UPDATE "users" SET "age" = age * '2' + '100', "updated_at" = '2020-02-16 13:05:51' WHERE `users`.`id` = 1;
  db.Model(&user).Updates(map[string]interface{}{"age": gorm.Expr("age * ? + ?", 2, 100)})
  // UPDATE "users" SET "age" = age - 1 WHERE "id" = '1';
  db.Model(&user).UpdateColumn("age", gorm.Expr("age - ?", 1))
  // UPDATE "users" SET "age" = age - 1 WHERE "id" = '1' AND age > 10;
  db.Model(&user).Where("age > 10").UpdateColumn("age", gorm.Expr("age - ?", 1))


目录
相关文章
|
5天前
|
SQL 数据库
GORM—删
GORM—删
41 0
|
5天前
|
SQL 数据库 索引
gorm普通的增删改查
gorm普通的增删改查
33 0
|
5天前
|
JSON 数据库 数据格式
gorm 自定义数据的使用
gorm 自定义数据的使用
19 0
|
5天前
|
SQL 数据库
GORM—查
GORM—查
37 0
|
5天前
|
SQL 数据库
GORM—增
GORM—增
28 0
|
5天前
|
5天前
|
数据库
gorm 教程 一(2)
gorm 教程 一
49 0
|
5天前
|
SQL 数据库
|
5天前
|
SQL 数据库
gorm 教程 一(3)
gorm 教程 一
69 0
|
5天前
|
SQL Go 数据库
gorm 教程 一(1)
gorm 教程 一
46 0