{"record":{"id":"0e50e4155be05544","repo":"flipped-aurora/gin-vue-admin","slug":"s-w-0e50e4","errorCode":null,"errorMessage":"创建自动代码导出模板 %s 失败: %w","messagePattern":"创建自动代码导出模板 (.+?) 失败: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/auto_code_persistence.go","lineNumber":143,"sourceCode":"\tfor _, field := range info.Fields {\n\t\tif field != nil && field.Excel {\n\t\t\tfields[field.ColumnName] = field.FieldDesc\n\t\t}\n\t}\n\ttemplateInfo, err := json.Marshal(fields)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"序列化自动代码导出模板失败: %w\", err)\n\t}\n\tname := info.Package + \"_\" + info.StructName\n\tentity := model.SysExportTemplate{\n\t\tDBName:       info.BusinessDB,\n\t\tName:         name,\n\t\tTableName:    info.TableName,\n\t\tTemplateID:   name,\n\t\tTemplateInfo: string(templateInfo),\n\t}\n\tif err = tx.Create(&entity).Error; err != nil {\n\t\treturn fmt.Errorf(\"创建自动代码导出模板 %s 失败: %w\", name, err)\n\t}\n\thistory.ExportTemplateID = entity.ID\n\treturn nil\n}\n\nfunc autoCodeIdentityExists(ctx context.Context, db *gorm.DB, info request.AutoCode) (bool, error) {\n\tif db == nil {\n\t\treturn false, errors.New(\"自动代码数据库未初始化\")\n\t}\n\tvar count int64\n\terr := db.WithContext(ctx).Model(&model.SysAutoCodeHistory{}).\n\t\tWhere(\n\t\t\t\"business_db = ? AND (struct_name = ? OR abbreviation = ?) AND package = ? AND flag = ?\",\n\t\t\tinfo.BusinessDB,\n\t\t\tinfo.StructName,\n\t\t\tinfo.Abbreviation,\n\t\t\tinfo.Package,\n\t\t\t0,","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/auto_code_persistence.go#L125-L161","documentation":"Thrown by persistAutoCodeExportTemplate when tx.Create(&entity) fails to insert the SysExportTemplate row generated alongside auto-created code. The error is wrapped with the template name (Package_StructName).","triggerScenarios":"INSERT into sys_export_template fails during auto-code Create: duplicate TemplateID/name (Package_StructName already exists from a previous generation), schema drift on sys_export_template, or DB connection failure.","commonSituations":"Regenerating code for the same package/struct after an earlier run left an export template behind; manual deletion of the menu but not its export template causing unique-key conflicts; missing migration of sys_export_template on a fresh environment.","solutions":["Check the wrapped %w cause; on duplicate key, remove the stale sys_export_template row with TemplateID = Package_StructName or enable the auto-remove flow.","Run migrations so sys_export_template matches the current model.","Retry the generation; the transaction guarantees no partial template row persists."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"var cnt int64\ndb.Model(&system.SysExportTemplate{}).\n    Where(\"template_id = ?\", pkg+\"_\"+structName).Count(&cnt)\nif cnt > 0 { /* remove stale template first */ }","typeGuard":null,"tryCatchPattern":"if err := createAutoCode(req); err != nil {\n    if strings.Contains(err.Error(), \"Duplicate entry\") {\n        db.Where(\"template_id = ?\", name).Delete(&system.SysExportTemplate{})\n        err = createAutoCode(req)\n    }\n}","preventionTips":["Use the built-in auto-remove flow when regenerating the same struct.","Migrate sys_export_template on deployments.","Avoid manually deleting menus without their export templates."],"tags":["gorm","insert","auto-code","export-template"],"backgroundTag":"db-insert-constraint-violation","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}