参考 删除 | GORM - The fantastic ORM library for Golang, aims to be developer friendly.
// 删除数据 https://gorm.io/zh_CN/docs/delete.html
// 如果模型包含了一个 gorm.deletedat 字段(gorm.Model 已经包含了该字段),它将自动获得软删除的能力!
// 拥有软删除能力的模型调用 Delete 时,记录不会被数据库。但 GORM 会将 DeletedAt 置为当前时间, 并且不能再通过普通的查询方法找到该记录
// 软删除 UPDATE `t_test_user` SET `deleted_at`='2022-03-27 14:02:48.259' WHERE id = 10 AND `t_test_user`.`deleted_at` IS NULL
db.Where("id = ?", 10).Delete(&TestUser{})
// 物理删除 DELETE FROM `t_test_user` WHERE `t_test_user`.`id` = 11
db.Unscoped().Delete(&TestUser{ID: 11})
// UPDATE `t_test_user` SET `deleted_at`='2022-03-27 14:06:03.712' WHERE id = 12 AND `t_test_user`.`deleted_at` IS NULL
db.Where("id = ?", 12).Delete(&TestUser{})
// UPDATE `t_test_user` SET `deleted_at`='2022-03-27 14:06:51.373' WHERE `t_test_user`.`id` = 13 AND `t_test_user`.`deleted_at` IS NULL
db.Delete(&TestUser{}, 13)
// DELETE FROM `t_test_user` WHERE `t_test_user`.`id` = 14
db.Unscoped().Delete(&TestUser{}, 14)
// DELETE FROM `t_test_user` WHERE `t_test_user`.`id` IN (15,16,17)
db.Unscoped().Delete(&TestUser{}, []int{15, 16, 17})
db.Unscoped().Delete(&TestUser{}, []int{288})
// 批量删除
// UPDATE `t_test_user` SET `deleted_at`='2022-03-27 14:10:44.596' WHERE id in (18,19,20) AND `t_test_user`.`deleted_at` IS NULL
db.Where("id in ?", []int{18, 19, 20}).Delete(&TestUser{})
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)