当前位置:网站首页>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
}
边栏推荐
- 2019强网杯upload
- Effect Hook
- 博云容器云、DevOps 平台斩获可信云“技术最佳实践奖”
- Prometheus node exporter common monitoring indicators
- 背包问题dp
- TCP three handshakes and four disconnects
- Ref Hook
- CDH cluster integration external Flink (improved version - keep pace with the times)
- Hash table questions (Part 2)
- 好用移动APP自动化测试框架哪里找?收藏这份清单就好了!
猜你喜欢
随机推荐
Effect Hook
地平线 旭日X3 PI (四) 板上运行(未写完)
TCP three handshakes and four disconnects
Cloudcompare & PCL matching point median (or standard deviation) distance suppression
Cloudcompare & PCL match point distance suppression
els_ Rectangle drawing, code planning and backup
The price reduction of iphone13 is just a show. Consumers are waiting for iphone14
日落红暖色调调色滤镜luts预设Sunset LUTs 1
【AtCoder Beginner Contest 260 (A·B·C)】
Skywalking distributed system application performance monitoring tool - medium
Qstring conversion char*
[day02] Introduction to data type conversion, operators and methods
CEPH operation
HCIA dynamic routing rip basic experiment
Prometheus node exporter common monitoring indicators
Chapter 6: cloud database
Summary of fire safety training materials
小程序项目如何创建
结构型模式-适配器模式
CDH集群集成外部Flink(改进版-与时俱进)







