after Update nil point panic

This commit is contained in:
leiyuqing 2022-04-03 12:12:45 +08:00
parent 201bf96f8e
commit f07e779830
2 changed files with 4 additions and 4 deletions

View File

@ -107,9 +107,9 @@ func Update(config *Config) func(db *gorm.DB) {
// AfterUpdate after update hooks // AfterUpdate after update hooks
func AfterUpdate(db *gorm.DB) { func AfterUpdate(db *gorm.DB) {
if !db.Statement.ReflectValue.CanAddr() { //if !db.Statement.ReflectValue.CanAddr() {
return // return
} //}
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterUpdate) { if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterUpdate) {
callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) { callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) {

View File

@ -255,7 +255,7 @@ func TestUseDBInHooks(t *testing.T) {
} }
testPanic := Product2{Name: "testPanic", Price: 100} testPanic := Product2{Name: "testPanic", Price: 100}
DB.Create(testPanic) DB.Create(&testPanic)
DB.Model(Product2{}).Where("name", "testPanic").Update("name", "testPanic1") DB.Model(Product2{}).Where("name", "testPanic").Update("name", "testPanic1")
var result Product2 var result Product2