当前位置:网站首页>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
}
边栏推荐
- Technology sharing | gtid that needs to be configured carefully_ mode
- Hash table questions (Part 2)
- Simple static routing in ENSP
- 5. Display of component dynamic components
- Yolov4网络详解
- redux三大核心
- How can I index the Oracle database field date?
- Redis interview question (2022)
- Prometheus node exporter common monitoring indicators
- JS tips
猜你喜欢

ps怎么导入lut预设?Photoshop导入lut调色预设教程
Full revolutionary networks for semantic segmentation (FCN)

利用Power Automate,轻松下载Power BI报告中的数据

Find a specific number in an ordered array

【C语言】自定义类型详解(结构体+枚举+联合)

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

JS day 2 (variables, variable usage, naming rules, syntax extensions)

HCIA static routing comprehensive experiment

可视化领域 SVG

Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
随机推荐
QString转换char*
Effect Hook
ELS compatibility DC, transfer pictures to window
els_ Rectangle drawing, code planning and backup
Cloudcompare & PCL match point distance suppression
ELS square display principle
Summary of fire safety training materials
ps怎么导入lut预设?Photoshop导入lut调色预设教程
Chapter 6: cloud database
干货 | 独立站运营怎么提高在线聊天客户服务?
Ref Hook
ceph操作
How to do smooth data migration: Double write scheme
Cloudcompare & PCL matching point median (or standard deviation) distance suppression
Photoshop裁剪工具隐藏技巧
结构型模式-适配器模式
详解左值、右值、左值引用以及右值引用
shell编程增强
安全第四次课后练习
The data in echart histogram is displayed at the top of the chart