{"record":{"id":"ae94acb1af27b70f","repo":"flipped-aurora/gin-vue-admin","slug":"gorm-bizmodel","errorCode":null,"errorMessage":"gorm 注入目标缺少 bizModel 函数","messagePattern":"gorm 注入目标缺少 bizModel 函数","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ast/package_initialize_gorm.go","lineNumber":95,"sourceCode":"\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn true\n\t})\n\n\tif packageNameNum == 1 {\n\t\t_ = NewImport(a.ImportPath).Rollback(file)\n\t}\n\treturn nil\n}\n\nfunc (a *PackageInitializeGorm) Injection(file *ast.File) error {\n\tif file == nil {\n\t\treturn fmt.Errorf(\"gorm 注入目标不能为空\")\n\t}\n\tbizModelDecl := FindFunction(file, \"bizModel\")\n\tif bizModelDecl == nil || bizModelDecl.Body == nil {\n\t\treturn fmt.Errorf(\"gorm 注入目标缺少 bizModel 函数\")\n\t}\n\tif a.Business != \"\" {\n\t\tif err := a.addDbVar(bizModelDecl.Body); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\tfound := false\n\t// 寻找目标结构\n\tast.Inspect(file, func(n ast.Node) bool {\n\t\t// 总调用的db变量根据business来决定\n\t\tvarDB := a.Business + \"Db\"\n\n\t\tif a.Business == \"\" {\n\t\t\tvarDB = \"db\"\n\t\t}\n\n\t\tcallExpr, ok := n.(*ast.CallExpr)\n\t\tif !ok {","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/ast/package_initialize_gorm.go#L77-L113","documentation":"PackageInitializeGorm.Injection requires the target initialize file to contain a function named bizModel that registers AutoMigrate calls. This error is returned when FindFunction(file, \"bizModel\") finds no such declaration, or the found declaration has a nil body. The injector has no anchor point to insert the model registration.","triggerScenarios":"Running the GORM codegen against an initialize/gorm.go file that is missing the bizModel function (older template, hand-rewritten file, or wrong Path/RelativePath pointing to a different file).","commonSituations":"Project scaffolding predates the bizModel convention; user renamed bizModel; Path/RelativePath misconfigured so the wrong file is parsed; the file was customized and the function deleted.","solutions":["Ensure the target file (e.g. server/initialize/gorm.go) defines 'func bizModel()' containing the AutoMigrate calls","Verify PackageInitializeGorm.Path/RelativePath points at the correct initialize file","Restore the bizModel function from the project template if it was removed","Re-run the injection after the function exists"],"exampleFix":"// before: target file has no bizModel\n// after: add the anchor function\nfunc bizModel() {\n    db.AutoMigrate()\n}","handlingStrategy":"validation","validationCode":"fset := token.NewFileSet()\nf, err := parser.ParseFile(fset, gormGoPath, nil, 0)\nif err == nil && ast.FindFunc(f, \"bizModel\") == nil {\n    return fmt.Errorf(\"%s has no bizModel function\", gormGoPath)\n}","typeGuard":null,"tryCatchPattern":"if err := inj.Injection(file); err != nil {\n    if strings.Contains(err.Error(), \"缺少 bizModel\") {\n        // regenerate initialize/gorm.go from the project template, then retry\n        return restoreTemplateAndRetry(path)\n    }\n    return err\n}","preventionTips":["Keep the standard bizModel function in server/initialize/gorm.go; don't rename or delete it","Point Path/RelativePath at the correct initialize file before injecting","When upgrading gin-vue-admin, diff initialize/gorm.go against the new template"],"tags":["go","ast","gorm","codegen","missing-anchor"],"backgroundTag":"missing-bizmodel-function","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}