当前位置:网站首页>On the problem that Gorm's beforedelete hook method does not work
On the problem that Gorm's beforedelete hook method does not work
2022-07-27 04:54:00 【dorlolo】
The following methods , Hook events will not be triggered .
because gorm Execution time , Will judge the incoming user Object has no data , If not, the hook event will be skipped .
db.Delete(&User{
}, 1)
//var ids = []uint{1,2,3}
//db.Where("id in ?",ids).Delete(&User{})
//db.Delete(&User{}, "id in ?", ids)
After testing , If you give it casually user Add some data to the object ,BeforeDelete The method will be triggered , Data can also be deleted correctly
var user= User{
Name: " Zhang San "}
err = global.GVA_DB.Where("id in ?", ids.Ids).Delete(&msg).Error
If you want to trigger a hook event , You can refer to the following method .
// Single delete
db..Delete(&user{
ID:1})
// Multiple deletions
var user []user
var ids = []uint{
1,2,3}
db.Where("id in ?", ids).Find(&user).Delete(&user)
The most basic , To delete a piece of data , At least id Have value . If you want to do some data processing through hook events , You can refer to the above items to delete , Calling Delete Before the method , Use first or find Methods take out the data first . In this way, you can directly get the value in the hook event to use
func (u *User) BeforeDelete(tx *gorm.DB) (err error) {
fmt.Printf(" Deleting user %v\n",u.Name)
return
}
边栏推荐
- Unity:Resource Merging、Static Batching、Dynamic Batching、GPU Instancing
- Ffmpeg merge video function
- JS第二天(变量、变量的使用、命名规则、语法扩展)
- 结构型模式-装饰者模式
- Dry goods | how can independent station operation improve online chat customer service?
- 有趣的C语言
- 【动态规划百题强化计划】11~20(持续更新中)
- 【AtCoder Beginner Contest 260 (A·B·C)】
- How to do smooth data migration: Double write scheme
- Ref Hook
猜你喜欢
![[C language] detailed explanation of user-defined types (structure + enumeration + Union)](/img/d9/b10371159c63c126b5ff98bac0971a.png)
[C language] detailed explanation of user-defined types (structure + enumeration + Union)
![[day02] Introduction to data type conversion, operators and methods](/img/81/e2c49a4206e5d0d05308a1fc881626.png)
[day02] Introduction to data type conversion, operators and methods

Structural mode - adapter mode

Digital integrated circuit: MOS tube device chapter (I)

Session&Cookie&token

HCIA dynamic routing OSPF experiment

Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"

ceph操作
![[C language] dynamic memory management](/img/20/3970cd2112204774a37b5a1d3bdce0.png)
[C language] dynamic memory management

数组中的最大值,最小值,冒泡排序
随机推荐
IP第十四天笔记
【C语言】自定义类型详解(结构体+枚举+联合)
第4章 Bean对象的作用域以及生命周期
HCIA dynamic routing OSPF experiment
els_ Rectangle drawing, code planning and backup
Yolov4 network details
Dynamic routing configuration
Two way republication experiment
干货 | 独立站运营怎么提高在线聊天客户服务?
结构型模式-桥接模式
STL upper series - detailed explanation of list container
Database leader Wang Shan: strive for innovation and carefully Polish high-quality database products
[day02] Introduction to data type conversion, operators and methods
Structural mode - facade mode
HCIA dynamic routing rip basic experiment
grid布局
Chapter 4 scope and life cycle of bean object
Ref Hook
勤于奋聊聊现在还有哪些副业可以做
The price reduction of iphone13 is just a show. Consumers are waiting for iphone14