{"record":{"id":"36f2cdf39a1d4811","repo":"flipped-aurora/gin-vue-admin","slug":"bizmodel-s-automigrate","errorCode":null,"errorMessage":"bizModel 中未找到 %s.AutoMigrate 调用","messagePattern":"bizModel 中未找到 (.+?)\\.AutoMigrate 调用","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ast/package_initialize_gorm.go","lineNumber":140,"sourceCode":"\n\t\t// 检查调用方是不是 db\n\t\tident, ok := selExpr.X.(*ast.Ident)\n\t\tif !ok || ident.Name != varDB {\n\t\t\treturn true\n\t\t}\n\n\t\tfound = true\n\t\t// 添加结构体参数\n\t\tcallExpr.Args = append(callExpr.Args, &ast.CompositeLit{\n\t\t\tType: &ast.SelectorExpr{\n\t\t\t\tX:   ast.NewIdent(a.PackageName),\n\t\t\t\tSel: ast.NewIdent(a.StructName),\n\t\t\t},\n\t\t})\n\t\treturn false\n\t})\n\tif !found {\n\t\treturn fmt.Errorf(\"bizModel 中未找到 %s.AutoMigrate 调用\", a.autoMigrateDBName())\n\t}\n\tif err := NewImport(a.ImportPath).Injection(file); err != nil {\n\t\treturn fmt.Errorf(\"注入 gorm import 失败: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc (a *PackageInitializeGorm) Format(filename string, writer io.Writer, file *ast.File) error {\n\tif filename == \"\" {\n\t\tfilename = a.Path\n\t}\n\treturn a.Base.Format(filename, writer, file)\n}\n\nfunc (a *PackageInitializeGorm) autoMigrateDBName() string {\n\tif a.Business == \"\" {\n\t\treturn \"db\"\n\t}","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/ast/package_initialize_gorm.go#L122-L158","documentation":"After ensuring the db variable exists, Injection scans the file for a CallExpr of the form <varDB>.AutoMigrate(...) where varDB is \"db\" (Business==\"\") or \"<Business>Db\". If no such call exists, the injector cannot append the new model argument and returns this error naming the expected receiver.","triggerScenarios":"The bizModel function exists but contains no matching AutoMigrate call: the call uses a differently named receiver variable, AutoMigrate is called on a method receiver or chained expression (not a plain ident), or AutoMigrate was removed from bizModel.","commonSituations":"Business field set to \"business\" but bizModel still calls db.AutoMigrate instead of businessDb.AutoMigrate; AutoMigrate wrapped in another function so the ident receiver doesn't match; hand-edited bizModel that dropped the call.","solutions":["Ensure bizModel contains a direct call like db.AutoMigrate() or businessDb.AutoMigrate() matching your Business setting","If Business is set, make sure the receiver variable is exactly <Business>Db (e.g. businessDb), not db","Avoid chaining the receiver (e.g. global.GVA_DB.WithContext(ctx).AutoMigrate) inside bizModel — use a plain ident variable","Re-run injection after fixing the call"],"exampleFix":"// before (Business=\"business\")\nfunc bizModel() {\n    db.AutoMigrate()\n}\n// after\nfunc bizModel() {\n    businessDb := global.GetGlobalDBByDBName(\"business\")\n    businessDb.AutoMigrate()\n}","handlingStrategy":"validation","validationCode":"// ensure bizModel has a matching plain-ident AutoMigrate call before injecting\nvarDB := \"db\"\nif business != \"\" { varDB = business + \"Db\" }\n// file must contain: <varDB>.AutoMigrate(...) with <varDB> as a bare identifier","typeGuard":null,"tryCatchPattern":"if err := inj.Injection(file); err != nil {\n    if strings.Contains(err.Error(), \"未找到\") {\n        return fmt.Errorf(\"add %s.AutoMigrate() to bizModel before running codegen: %w\", varDB, err)\n    }\n    return err\n}","preventionTips":["Call AutoMigrate on a plain local variable (db or <Business>Db), not a chained expression","Match the receiver name to the Business field: Business=\"business\" requires businessDb.AutoMigrate","Keep AutoMigrate calls directly inside bizModel, not behind wrapper functions"],"tags":["go","ast","gorm","automigrate","codegen"],"backgroundTag":"missing-automigrate-call","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}