当前位置:网站首页>关于gorm的BeforeDelete钩子方法不生效的问题
关于gorm的BeforeDelete钩子方法不生效的问题
2022-07-27 04:29:00 【dorlolo】
下面的几种方法,不会触发钩子事件.
因为gorm执行时,会判断传入的user对象没有数据,如果没有则会跳过钩子事件。
db.Delete(&User{
}, 1)
//var ids = []uint{1,2,3}
//db.Where("id in ?",ids).Delete(&User{})
//db.Delete(&User{}, "id in ?", ids)
经过测试,如果随便给user对象加点数据,BeforeDelete方法就会被触发,数据也能被正确删除
var user= User{
Name: "张三"}
err = global.GVA_DB.Where("id in ?", ids.Ids).Delete(&msg).Error
如果要触发钩子事件,可以参考下面的方法。
//单条删除
db..Delete(&user{
ID:1})
//多条删除
var user []user
var ids = []uint{
1,2,3}
db.Where("id in ?", ids).Find(&user).Delete(&user)
最基本的,要删除一条数据,至少id要有值。如果要通过钩子事件做一些数据处理,可以参考上面的多条删除,在调用Delete方法前,使用first或find方法先拿出数据.这样方法在钩子事件中直接拿到值来使用
func (u *User) BeforeDelete(tx *gorm.DB) (err error) {
fmt.Printf("正在删除用户%v\n",u.Name)
return
}
边栏推荐
- Influxdb basic understanding
- 深度学习领域图像分割FCN(Fully Convolutional Networks for Semantic Segmentation)
- 微信小程序编辑头像
- Understand kingbasees V9 in one picture
- 详解左值、右值、左值引用以及右值引用
- Dry goods | how can independent station operation improve online chat customer service?
- Use unity to build a WordArt system
- From scratch, C language intensive Lecture 4: array
- 【AtCoder Beginner Contest 260 (A·B·C)】
- 【HCIP】重发布、重分布、重分发实验
猜你喜欢

Three cores of Redux

第4章 Bean对象的作用域以及生命周期

grid布局
![Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk](/img/ed/941276a15d1c4ab67d397fb3286022.png)
Text processing tool in shell, cut [option parameter] filename Description: the default separator is the built-in variable of tab, awk [option parameter] '/pattern1/{action1}filename and awk

Grid layout

Unity:Resource Merging、Static Batching、Dynamic Batching、GPU Instancing

Find a specific number in an ordered array

第六章:云数据库

STL 上头系列——list 容器详解

Scala immutable map, variable map, map conversion to other data types
随机推荐
IIC 通信协议 (一)
Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products
Easy to use shell shortcuts
Easy to use mobile app automation testing framework where to find? Just collect this list!
ELS compatibility DC, transfer pictures to window
Eureka service registry
打开编程的大门
好用移动APP自动化测试框架哪里找?收藏这份清单就好了!
Dino paper accuracy, and analyze the variant of its model structure & Detr
Spark practice case (upgraded version)
QString转换char*
Differences and uses of SRAM, DRAM, SDRAM and DDR
Shell的正则表达式入门、常规匹配、特殊字符:^、$、.、*、字符区间(中括号):[ ]、特殊字符:\、匹配手机号
Pinia入门到精通,Pinia使用全流程,包含state,actions,getters,以及如何解构,进行响应,actions使用的多种方法
Safety fourth after class exercise
VSCode开启Pull Request更新代码分支可视化新篇章
pinia的持久化存储,pinia使用插件进行持久化存储。
Pinia uses the whole process, including state, actions, getters, and how to deconstruct, respond, and various methods used by actions
冒泡排序(详细)
Shel automatically sets directory permissions