flipped-aurora/gin-vue-admin · error

bizModel 中未找到 %s.AutoMigrate 调用

Error message

bizModel 中未找到 %s.AutoMigrate 调用

What it means

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.

Source

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

		// 检查调用方是不是 db
		ident, ok := selExpr.X.(*ast.Ident)
		if !ok || ident.Name != varDB {
			return true
		}

		found = true
		// 添加结构体参数
		callExpr.Args = append(callExpr.Args, &ast.CompositeLit{
			Type: &ast.SelectorExpr{
				X:   ast.NewIdent(a.PackageName),
				Sel: ast.NewIdent(a.StructName),
			},
		})
		return false
	})
	if !found {
		return fmt.Errorf("bizModel 中未找到 %s.AutoMigrate 调用", a.autoMigrateDBName())
	}
	if err := NewImport(a.ImportPath).Injection(file); err != nil {
		return fmt.Errorf("注入 gorm import 失败: %w", err)
	}
	return nil
}

func (a *PackageInitializeGorm) Format(filename string, writer io.Writer, file *ast.File) error {
	if filename == "" {
		filename = a.Path
	}
	return a.Base.Format(filename, writer, file)
}

func (a *PackageInitializeGorm) autoMigrateDBName() string {
	if a.Business == "" {
		return "db"
	}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Ensure bizModel contains a direct call like db.AutoMigrate() or businessDb.AutoMigrate() matching your Business setting
  2. If Business is set, make sure the receiver variable is exactly <Business>Db (e.g. businessDb), not db
  3. Avoid chaining the receiver (e.g. global.GVA_DB.WithContext(ctx).AutoMigrate) inside bizModel — use a plain ident variable
  4. Re-run injection after fixing the call

Example fix

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

Strategy: validation

Validate before calling

// ensure bizModel has a matching plain-ident AutoMigrate call before injecting
varDB := "db"
if business != "" { varDB = business + "Db" }
// file must contain: <varDB>.AutoMigrate(...) with <varDB> as a bare identifier

Try / catch

if err := inj.Injection(file); err != nil {
    if strings.Contains(err.Error(), "未找到") {
        return fmt.Errorf("add %s.AutoMigrate() to bizModel before running codegen: %w", varDB, err)
    }
    return err
}

Prevention

When it happens

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

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

Related errors


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