From 68fa604e915215c6176b6917d5cb1dbbda1ccb46 Mon Sep 17 00:00:00 2001 From: demoManito <1430482733@qq.com> Date: Mon, 21 Jul 2025 16:34:23 +0800 Subject: [PATCH] fix --- schema/schema.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index 53a000be..1ab27125 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "go/ast" + "path" "reflect" "strings" "sync" @@ -314,10 +315,12 @@ func ParseWithSpecialTableName(dest interface{}, cacheStore *sync.Map, namer Nam if methodValue := callBackToMethodValue(modelValue, cbName); methodValue.IsValid() { switch methodValue.Type().String() { case "func(*gorm.DB) error": - if inVarPkg := methodValue.Type().In(0).Elem().PkgPath(); inVarPkg != "gorm.io/gorm" { - logger.Default.Warn(context.Background(), "In model %v, the hook function `%v(*gorm.DB) error` has an incorrect parameter type. The expected parameter type is `gorm.io/gorm`, but the provided type is `%v`.", schema, cbName, inVarPkg) - } else { + expectedPkgPath := path.Dir(reflect.TypeOf(schema).Elem().PkgPath()) + if inVarPkg := methodValue.Type().In(0).Elem().PkgPath(); inVarPkg == expectedPkgPath { reflect.Indirect(reflect.ValueOf(schema)).FieldByName(string(cbName)).SetBool(true) + } else { + logger.Default.Warn(context.Background(), "In model %v, the hook function `%v(*gorm.DB) error` has an incorrect parameter type. The expected parameter type is `%v`, but the provided type is `%v`.", schema, cbName, expectedPkgPath, inVarPkg) + // PASS } default: logger.Default.Warn(context.Background(), "Model %v don't match %vInterface, should be `%v(*gorm.DB) error`. Please see https://gorm.io/docs/hooks.html", schema, cbName, cbName)