{"record":{"id":"cc641fa15a955c0c","repo":"flipped-aurora/gin-vue-admin","slug":"error-cc641f","errorCode":null,"errorMessage":"注册表失败!","messagePattern":"注册表失败!","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/ast/plugin_initialize_gorm.go","lineNumber":155,"sourceCode":"\nfunc (a *PluginInitializeGorm) isTargetAutoMigrateCall(callExpr *ast.CallExpr) bool {\n\tselExpr, ok := callExpr.Fun.(*ast.SelectorExpr)\n\tif !ok || selExpr.Sel.Name != \"AutoMigrate\" {\n\t\treturn false\n\t}\n\treturn exprString(selExpr.X) == exprString(a.autoMigrateReceiverExpr())\n}\n\nfunc (a *PluginInitializeGorm) appendAutoMigrateBlock(file *ast.File) *ast.CallExpr {\n\tgormFunc := FindFunction(file, \"Gorm\")\n\tif gormFunc == nil || gormFunc.Body == nil {\n\t\treturn nil\n\t}\n\n\tsrc := fmt.Sprintf(`package placeholder\nfunc Gorm() {\n\tif err = %s.AutoMigrate(); err != nil {\n\t\terr = errors.Wrap(err, \"注册表失败!\")\n\t\tzap.L().Error(fmt.Sprintf(\"%%+v\", err))\n\t}\n}\n`, exprString(a.autoMigrateReceiverExpr()))\n\n\tparsed, err := parser.ParseFile(token.NewFileSet(), \"\", src, 0)\n\tif err != nil || len(parsed.Decls) == 0 {\n\t\treturn nil\n\t}\n\n\tstmt := parsed.Decls[0].(*ast.FuncDecl).Body.List[0].(*ast.IfStmt)\n\tclearPosition(stmt)\n\tgormFunc.Body.List = append(gormFunc.Body.List, stmt)\n\n\tassignStmt := stmt.Init.(*ast.AssignStmt)\n\tcallExpr := assignStmt.Rhs[0].(*ast.CallExpr)\n\treturn callExpr\n}","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/ast/plugin_initialize_gorm.go#L137-L173","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before (generated)\nif err = biz.Plugin{}.AutoMigrate(); err != nil { ... }\n// after\n// ensure model embeds gorm.Model and is registered:\nerr = global.GVA_DB.AutoMigrate(&biz.Plugin{})\nif err != nil { panic(err) }","handlingStrategy":"validation","validationCode":"if err := global.GVA_DB.Exec(\"SELECT 1\").Error; err != nil { return fmt.Errorf(\"db unreachable before migrate: %w\", err) }","typeGuard":null,"tryCatchPattern":"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","preventionTips":["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)"],"tags":["gorm","migration","codegen","startup"],"backgroundTag":"automigrate-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}