flipped-aurora/gin-vue-admin · error
注册表失败!
Error message
注册表失败!
What it means
This message originates inside the generated AutoMigrate block that Injection appends to register_gorm.go: at runtime, when the generated Gorm() runs `tx.AutoMigrate(...)` it fails and the generated code wraps the GORM error with '注册表失败!'. Note the SOURCE shown here is the template string the injector produces, so the error appears in generated code, not in plugin_initialize_gorm.go itself.
Source
Thrown at server/utils/ast/plugin_initialize_gorm.go:155
func (a *PluginInitializeGorm) isTargetAutoMigrateCall(callExpr *ast.CallExpr) bool {
selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
if !ok || selExpr.Sel.Name != "AutoMigrate" {
return false
}
return exprString(selExpr.X) == exprString(a.autoMigrateReceiverExpr())
}
func (a *PluginInitializeGorm) appendAutoMigrateBlock(file *ast.File) *ast.CallExpr {
gormFunc := FindFunction(file, "Gorm")
if gormFunc == nil || gormFunc.Body == nil {
return nil
}
src := fmt.Sprintf(`package placeholder
func Gorm() {
if err = %s.AutoMigrate(); err != nil {
err = errors.Wrap(err, "注册表失败!")
zap.L().Error(fmt.Sprintf("%%+v", err))
}
}
`, exprString(a.autoMigrateReceiverExpr()))
parsed, err := parser.ParseFile(token.NewFileSet(), "", src, 0)
if err != nil || len(parsed.Decls) == 0 {
return nil
}
stmt := parsed.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.IfStmt)
clearPosition(stmt)
gormFunc.Body.List = append(gormFunc.Body.List, stmt)
assignStmt := stmt.Init.(*ast.AssignStmt)
callExpr := assignStmt.Rhs[0].(*ast.CallExpr)
return callExpr
}View on GitHub (pinned to 3136500ef3)
Solutions
- Read the wrapped underlying GORM error printed via zap (fmt.Sprintf("%+v", err)) to see the exact migration failure
- Verify the DB user has CREATE/ALTER privileges on the schema
- Check the registered model expression is correct and the struct compiles as a GORM model (valid primary key, supported types)
- Ensure dependent tables/models are registered before the one with the FK constraint, then restart
Example fix
// before (generated)
if err = biz.Plugin{}.AutoMigrate(); err != nil { ... }
// after
// ensure model embeds gorm.Model and is registered:
err = global.GVA_DB.AutoMigrate(&biz.Plugin{})
if err != nil { panic(err) } Defensive patterns
Strategy: validation
Validate before calling
if err := global.GVA_DB.Exec("SELECT 1").Error; err != nil { return fmt.Errorf("db unreachable before migrate: %w", err) } Try / catch
func Gorm() { if err = tx.AutoMigrate(models...); err != nil { err = errors.Wrap(err, "注册表失败!"); zap.L().Error(fmt.Sprintf("%+v", err)); } } — inspect the wrapped cause with errors.Unwrap / %+v Prevention
- Grant the app DB user CREATE/ALTER privileges needed by AutoMigrate
- Register models in dependency order so FK targets exist first
- Test migrations against the target dialect (sqlite vs mysql vs pgsql) in CI
- Keep model structs valid GORM models (embedded primary key, supported types)
When it happens
Trigger: Server startup with a model struct that cannot be migrated: missing database table privileges, unsupported column types for the configured dialect, a model referencing a missing foreign-key target, or the model type expression injected is wrong/empty so AutoMigrate receives an invalid target.
Common situations: Adding a new plugin/model and forgetting AutoMigrate prerequisites; MySQL user lacking CREATE/ALTER privileges; switching DB dialect (sqlite->mysql) where a field type is unsupported; duplicate/misordered initialization so the receiver variable is nil at migration time.
Related errors
- gorm 注入目标不能为空
- gorm 注入目标缺少 bizModel 函数
- bizModel 中未找到 %s.AutoMigrate 调用
- 注入 gorm import 失败: %w
- bizModel 函数体为空,无法注入业务数据库
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/cc641fa15a955c0c.
Report an issue: GitHub.