当前位置:网站首页>关于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
}
边栏推荐
- Interview must ask | what stages does a thread go through from creation to extinction?
- Network knowledge corner | it only takes four steps to teach you to use SecureCRT to connect to ENSP. You must see the operation guide of common tools
- Pinia uses the whole process, including state, actions, getters, and how to deconstruct, respond, and various methods used by actions
- 博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”
- Using webmvcconfigurer to intercept interface requests is being enhanced (with source code)
- Nacos startup and login
- Solution to the third game of 2022 Hangzhou Electric Multi school league
- Prometheus node exporter common monitoring indicators
- 0动态规划中等 LeetCode467. 环绕字符串中唯一的子字符串
- RSA 非对称 加密解密 加签验签工具
猜你喜欢
![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

冒泡排序(详细)

There are two solutions for the feign call header of microservices to be discarded (with source code)

华为入局商用市场:趋势使然,挑战颇多

博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”

Interesting C language

BSN IPFs (interstellar file system) private network introduction, functions, architecture and characteristics, access instructions

Scala immutable map, variable map, map conversion to other data types

Anonymous named pipes, understanding and use of interprocess communication in shared memory

The data in echart histogram is displayed at the top of the chart
随机推荐
Do you know about wechat merchant billing?
Huawei's entry into the commercial market: due to the trend, there are many challenges
【独立站建设】跨境电商出海开网店,首选这个网站建设!
Nacos startup and login
Dry goods | how can independent station operation improve online chat customer service?
Dino paper accuracy, and analyze the variant of its model structure & Detr
Wechat applet rotation map
消防安全培训资料汇总
Eureka service registry
IP第十四天笔记
Some common instructions in JVM tuning
RSA 非对称 加密解密 加签验签工具
Grid layout
【动态规划百题强化计划】11~20(持续更新中)
shell编程增强
How does novice Xiaobai learn to be we media?
华为入局商用市场:趋势使然,挑战颇多
Ribbon load balancing principle and some source codes
Overview of communication protocols
使用Unity做一个艺术字系统