1. 删除

1.1. 删除记录

警告:当删除一条记录的时候,你需要确定这条记录的主键有值,GORM会使用主键来删除这条记录。如果主键字段为空,GORM会删除模型中所有的记录。

  1. // 删除一条存在的记录
  2. db.Delete(&email)
  3. //// DELETE from emails where id=10;
  4. // 为删除 SQL 语句添加额外选项
  5. db.Set("gorm:delete_option", "OPTION (OPTIMIZE FOR UNKNOWN)").Delete(&email)
  6. //// DELETE from emails where id=10 OPTION (OPTIMIZE FOR UNKNOWN);

1.2. 批量删除

删除所有匹配的记录

  1. db.Where("email LIKE ?", "%jinzhu%").Delete(Email{})
  2. //// DELETE from emails where email LIKE "%jinzhu%";
  3. db.Delete(Email{}, "email LIKE ?", "%jinzhu%")
  4. //// DELETE from emails where email LIKE "%jinzhu%";

1.3. 软删除

如果模型中有 DeletedAt 字段,它将自动拥有软删除的能力!当执行删除操作时,数据并不会永久的从数据库中删除,而是将 DeletedAt 的值更新为当前时间。

  1. db.Delete(&user)
  2. //// UPDATE users SET deleted_at="2013-10-29 10:23" WHERE id = 111;
  3. // 批量删除
  4. db.Where("age = ?", 20).Delete(&User{})
  5. //// UPDATE users SET deleted_at="2013-10-29 10:23" WHERE age = 20;
  6. // 在查询记录时,软删除记录会被忽略
  7. db.Where("age = 20").Find(&user)
  8. //// SELECT * FROM users WHERE age = 20 AND deleted_at IS NULL;
  9. // 使用 Unscoped 方法查找软删除记录
  10. db.Unscoped().Where("age = 20").Find(&users)
  11. //// SELECT * FROM users WHERE age = 20;
  12. // 使用 Unscoped 方法永久删除记录
  13. db.Unscoped().Delete(&order)
  14. //// DELETE FROM orders WHERE id=10;