{"record":{"id":"fd237df7464a9db4","repo":"flipped-aurora/gin-vue-admin","slug":"bizmodel","errorCode":null,"errorMessage":"bizModel 函数体为空，无法注入业务数据库","messagePattern":"bizModel 函数体为空，无法注入业务数据库","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ast/package_initialize_gorm.go","lineNumber":174,"sourceCode":"\tif a.Business == \"\" {\n\t\treturn \"db\"\n\t}\n\treturn a.Business + \"Db\"\n}\n\n// 创建businessDB变量\nfunc (a *PackageInitializeGorm) addDbVar(astBody *ast.BlockStmt) error {\n\tfor i := range astBody.List {\n\t\tif assignStmt, ok := astBody.List[i].(*ast.AssignStmt); ok {\n\t\t\tif ident, ok := assignStmt.Lhs[0].(*ast.Ident); ok {\n\t\t\t\tif (a.Business == \"\" && ident.Name == \"db\") || ident.Name == a.Business+\"Db\" {\n\t\t\t\t\treturn nil\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif len(astBody.List) == 0 {\n\t\treturn fmt.Errorf(\"bizModel 函数体为空，无法注入业务数据库\")\n\t}\n\n\t// 添加 businessDb := global.GetGlobalDBByDBName(\"business\") 变量\n\tassignNode := &ast.AssignStmt{\n\t\tLhs: []ast.Expr{\n\t\t\t&ast.Ident{\n\t\t\t\tName: a.Business + \"Db\",\n\t\t\t},\n\t\t},\n\t\tTok: token.DEFINE,\n\t\tRhs: []ast.Expr{\n\t\t\t&ast.CallExpr{\n\t\t\t\tFun: &ast.SelectorExpr{\n\t\t\t\t\tX: &ast.Ident{\n\t\t\t\t\t\tName: \"global\",\n\t\t\t\t\t},\n\t\t\t\t\tSel: &ast.Ident{\n\t\t\t\t\t\tName: \"GetGlobalDBByDBName\",","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/ast/package_initialize_gorm.go#L156-L192","documentation":"addDbVar creates the <Business>Db variable inside bizModel when Business is set. It first scans existing AssignStmts for an existing variable named <Business>Db (or db). If none exists AND the function body has zero statements, it cannot insert the assignment before the (missing) trailing return and returns this error.","triggerScenarios":"Injection called with Business != \"\" on a file whose bizModel body is completely empty (func bizModel() {}), so there is no statement to anchor the new assignment before.","commonSituations":"Freshly scaffolded gorm.go template whose bizModel is empty while Business is configured; user cleared out bizModel content; wrong file matched that happens to have an empty bizModel.","solutions":["Add at least one statement to bizModel — typically the AutoMigrate call (businessDb.AutoMigrate()) or an existing db variable assignment","Ensure the injection order: Injection normally adds the AutoMigrate call via bizModel content; keep a return or call statement as the anchor","If the template should be empty, either set Business=\"\" or seed bizModel with the default template body before injecting"],"exampleFix":"// before\nfunc bizModel() {}\n// after\nfunc bizModel() {\n    businessDb := global.GetGlobalDBByDBName(\"business\")\n    businessDb.AutoMigrate()\n}","handlingStrategy":"validation","validationCode":"// before injecting with Business set, ensure bizModel is non-empty\n// body must contain at least one statement (e.g. the AutoMigrate call)\nif len(bizModelBody.List) == 0 {\n    return fmt.Errorf(\"bizModel must not be empty\")\n}","typeGuard":null,"tryCatchPattern":"if err := inj.Injection(file); err != nil {\n    if strings.Contains(err.Error(), \"函数体为空\") {\n        return fmt.Errorf(\"seed bizModel with the template body before injecting: %w\", err)\n    }\n    return err\n}","preventionTips":["Never ship an empty bizModel in gorm.go; seed it from the project template","Set Business=\"\" if you don't actually use a named business database","When regenerating initialize files, always run the template's default content first"],"tags":["go","ast","gorm","codegen","empty-body"],"backgroundTag":"empty-function-body","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}