flipped-aurora/gin-vue-admin · error

bizModel 函数体为空,无法注入业务数据库

Error message

bizModel 函数体为空,无法注入业务数据库

What it means

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.

Source

Thrown at server/utils/ast/package_initialize_gorm.go:174

	if a.Business == "" {
		return "db"
	}
	return a.Business + "Db"
}

// 创建businessDB变量
func (a *PackageInitializeGorm) addDbVar(astBody *ast.BlockStmt) error {
	for i := range astBody.List {
		if assignStmt, ok := astBody.List[i].(*ast.AssignStmt); ok {
			if ident, ok := assignStmt.Lhs[0].(*ast.Ident); ok {
				if (a.Business == "" && ident.Name == "db") || ident.Name == a.Business+"Db" {
					return nil
				}
			}
		}
	}
	if len(astBody.List) == 0 {
		return fmt.Errorf("bizModel 函数体为空,无法注入业务数据库")
	}

	// 添加 businessDb := global.GetGlobalDBByDBName("business") 变量
	assignNode := &ast.AssignStmt{
		Lhs: []ast.Expr{
			&ast.Ident{
				Name: a.Business + "Db",
			},
		},
		Tok: token.DEFINE,
		Rhs: []ast.Expr{
			&ast.CallExpr{
				Fun: &ast.SelectorExpr{
					X: &ast.Ident{
						Name: "global",
					},
					Sel: &ast.Ident{
						Name: "GetGlobalDBByDBName",

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Add at least one statement to bizModel — typically the AutoMigrate call (businessDb.AutoMigrate()) or an existing db variable assignment
  2. Ensure the injection order: Injection normally adds the AutoMigrate call via bizModel content; keep a return or call statement as the anchor
  3. If the template should be empty, either set Business="" or seed bizModel with the default template body before injecting

Example fix

// before
func bizModel() {}
// after
func bizModel() {
    businessDb := global.GetGlobalDBByDBName("business")
    businessDb.AutoMigrate()
}
Defensive patterns

Strategy: validation

Validate before calling

// before injecting with Business set, ensure bizModel is non-empty
// body must contain at least one statement (e.g. the AutoMigrate call)
if len(bizModelBody.List) == 0 {
    return fmt.Errorf("bizModel must not be empty")
}

Try / catch

if err := inj.Injection(file); err != nil {
    if strings.Contains(err.Error(), "函数体为空") {
        return fmt.Errorf("seed bizModel with the template body before injecting: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/fd237df7464a9db4. Report an issue: GitHub.