当前位置:网站首页>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
}
边栏推荐
- Ref Hook
- Two way republication experiment
- JS tips
- Grid layout
- CDH cluster integration external Flink (improved version - keep pace with the times)
- VSCode开启Pull Request更新代码分支可视化新篇章
- 安全第四次课后练习
- 干货 | 独立站运营怎么提高在线聊天客户服务?
- Redis interview question (2022)
- [C language] detailed explanation of user-defined types (structure + enumeration + Union)
猜你喜欢

Vscode opens a new chapter in the visualization of pull request update code branches

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

Final review of management information system
![[final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)](/img/f4/70634556c4ae8fc3b087084e1e27b3.png)
[final review of software engineering] knowledge points + detailed explanation of major problems (E-R diagram, data flow diagram, N-S box diagram, state diagram, activity diagram, use case diagram...)
![[construction of independent stations] this website is the first choice for cross-border e-commerce companies to open online stores at sea!](/img/26/22597a7ece0431a4e54703945a775c.jpg)
[construction of independent stations] this website is the first choice for cross-border e-commerce companies to open online stores at sea!

「Photoshop2021入门教程」对齐与分布制作波点图案

消防安全培训资料汇总

Three cores of Redux

博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”
深度学习领域图像分割FCN(Fully Convolutional Networks for Semantic Segmentation)
随机推荐
可视化领域 SVG
双向重发布实验
RSA asymmetric encryption and decryption signature verification tool
[C language] dynamic memory management
Shell programming enhancements
Safety fourth after class exercise
STM32 serial port based on Hal library accepts interrupts and idle interrupts
State Hook
冒泡排序(详细)
Pinia uses plug-ins for persistent storage.
iPhone13再降价,其实只是做做样子,消费者都在等iPhone14
Digital integrated circuit: CMOS inverter (I) static characteristics
「Photoshop2021入门教程」对齐与分布制作波点图案
Photoshop裁剪工具隐藏技巧
安全第四次课后练习
RN development series < 9 > --mobx (1) Introduction
Final review of management information system
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
利用Power Automate,轻松下载Power BI报告中的数据