{"record":{"id":"07b1a93b5f367344","repo":"flipped-aurora/gin-vue-admin","slug":"gorm-gorm-automigrate","errorCode":null,"errorMessage":"插件 gorm 注入目标缺少 Gorm 函数或 AutoMigrate 调用","messagePattern":"插件 gorm 注入目标缺少 Gorm 函数或 AutoMigrate 调用","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ast/plugin_initialize_gorm.go","lineNumber":112,"sourceCode":"\n\t\tselExpr, ok := callExpr.Fun.(*ast.SelectorExpr)\n\t\tif !ok || selExpr.Sel.Name != \"AutoMigrate\" {\n\t\t\treturn true\n\t\t}\n\n\t\tif a.isTargetAutoMigrateCall(callExpr) {\n\t\t\ttargetCall = callExpr\n\t\t\treturn false\n\t\t}\n\n\t\treturn true\n\t})\n\n\tif targetCall == nil {\n\t\ttargetCall = a.appendAutoMigrateBlock(file)\n\t}\n\tif targetCall == nil {\n\t\treturn fmt.Errorf(\"插件 gorm 注入目标缺少 Gorm 函数或 AutoMigrate 调用\")\n\t}\n\n\tif a.hasModelArg(targetCall) {\n\t\treturn nil\n\t}\n\tif err := NewImport(a.ImportPath).Injection(file); err != nil {\n\t\treturn fmt.Errorf(\"注入插件 gorm import 失败: %w\", err)\n\t}\n\n\ttargetCall.Args = append(targetCall.Args, &ast.CompositeLit{\n\t\tType: &ast.SelectorExpr{\n\t\t\tX:   &ast.Ident{Name: a.PackageName},\n\t\t\tSel: &ast.Ident{Name: a.StructName},\n\t\t},\n\t})\n\treturn nil\n}\n","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/ast/plugin_initialize_gorm.go#L94-L130","documentation":"PluginInitializeGorm.Injection looks for an existing <db>.WithContext(ctx).AutoMigrate(...) call matching the configured receiver; if none exists it tries to append a new AutoMigrate block inside a function named Gorm (via appendAutoMigrateBlock). This error is returned when both strategies fail: no matching AutoMigrate call exists anywhere AND no Gorm function (with a body) exists in the file to receive the generated block.","triggerScenarios":"Running plugin GORM codegen against a gorm.go file that neither contains an AutoMigrate call with the expected receiver nor defines 'func Gorm()'; appendAutoMigrateBlock returns nil because FindFunction(file, \"Gorm\") is nil or has nil body, or its inline template parse fails.","commonSituations":"Plugin template predates the Gorm() convention; the Gorm function was renamed (e.g. to gormInit) or inlined; wrong Path/RelativePath so a different file is parsed; hand-rewritten plugin initialize file.","solutions":["Ensure the plugin's gorm.go defines 'func Gorm()' (with a non-nil body) containing or able to receive the AutoMigrate block, or add a matching <db>.WithContext(ctx).AutoMigrate() call","Verify PluginInitializeGorm.Path/RelativePath points at the plugin's gorm.go initialize file","Restore the standard Gorm() function from the plugin template if it was renamed or removed","Re-run the injection after the anchor exists"],"exampleFix":"// before: file has neither AutoMigrate nor Gorm\n// after: add the anchor function\nfunc Gorm() {\n    if err = global.GVA_DB.WithContext(ctx).AutoMigrate(); err != nil {\n        err = errors.Wrap(err, \"注册表失败!\")\n        zap.L().Error(fmt.Sprintf(\"%+v\", err))\n    }\n}","handlingStrategy":"validation","validationCode":"// before injecting, confirm the plugin gorm.go has a Gorm function or matching AutoMigrate call\nvarDBExpr := \"global.GVA_DB.WithContext(ctx)\"\nif business != \"\" { varDBExpr = fmt.Sprintf(\"global.MustGetGlobalDBByDBName(\\\"%s\\\").WithContext(ctx)\", business) }\n// file should contain <varDBExpr>.AutoMigrate(...) or define func Gorm() with a body","typeGuard":null,"tryCatchPattern":"if err := pluginInj.Injection(file); err != nil {\n    if strings.Contains(err.Error(), \"缺少 Gorm 函数\") {\n        return fmt.Errorf(\"restore the standard Gorm() function in the plugin gorm.go template: %w\", err)\n    }\n    return err\n}","preventionTips":["Keep the standard Gorm() function in each plugin's gorm.go; don't rename it","Use the expected receiver form (global.GVA_DB.WithContext(ctx).AutoMigrate or global.MustGetGlobalDBByDBName(...).WithContext(ctx).AutoMigrate) so detection matches","Verify Path/RelativePath resolves to the plugin's gorm.go before injecting","When customizing plugin initialization, preserve the Gorm() anchor or add your own matching AutoMigrate call"],"tags":["go","ast","gorm","plugin","missing-anchor"],"backgroundTag":"missing-gorm-anchor-function","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}