Skip to content

删除数据


Delete()

可以删除某些数据,注意参数是可选填,时间的字段名也可以通过全局去配置

软删除 Delete()

  • @param options 设置选项, 选填可不传 tg.DeleteOption{}
  • -------@param Debug 是否打印最终执行的SQL语句,默认不打印
  • -------@param IsDeleteFlag 是否是软删除,默认是
  • -------@param DeleteTime 删除时间字段名,默认 delete_time
  • -------@param DeleteProtect 删除保护,默认开启,防止忘记写WHERE条件误删除所有数据,只争对物理删除有效
err := tg.Db("user").Where("id", "=", 1).Delete()
err := tg.Db("user").Where("id", "=", 1).Delete()

最终的SQL语句为:

UPDATE user SET delete_time = '2023-12-12 00:00:00' WHERE id = 1
UPDATE user SET delete_time = '2023-12-12 00:00:00' WHERE id = 1

物理删除 Delete()

err := tg.Db("user").Where("id", "=", 1).Delete(tg.DeleteOption{ IsDeleteFlag: tgutl.PtrBool(false) })
err := tg.Db("user").Where("id", "=", 1).Delete(tg.DeleteOption{ IsDeleteFlag: tgutl.PtrBool(false) })

最终的SQL语句为:

DELETE FROM user WHERE id = 1
DELETE FROM user WHERE id = 1

备案号:冀ICP备20015584号-5