{"record":{"id":"18bea04273c0d6b8","repo":"flipped-aurora/gin-vue-admin","slug":"s-w-18bea0","errorCode":null,"errorMessage":"创建自动代码菜单 %s 失败: %w","messagePattern":"创建自动代码菜单 (.+?) 失败: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/auto_code_persistence.go","lineNumber":114,"sourceCode":"\n\tif info.AutoCreateBtnAuth && !info.OnlyTemplate {\n\t\tdesired.MenuBtn = []model.SysBaseMenuBtn{\n\t\t\t{Name: \"add\", Desc: \"新增\"},\n\t\t\t{Name: \"batchDelete\", Desc: \"批量删除\"},\n\t\t\t{Name: \"delete\", Desc: \"删除\"},\n\t\t\t{Name: \"edit\", Desc: \"编辑\"},\n\t\t\t{Name: \"info\", Desc: \"详情\"},\n\t\t}\n\t\tif info.HasExcel {\n\t\t\tdesired.MenuBtn = append(desired.MenuBtn,\n\t\t\t\tmodel.SysBaseMenuBtn{Name: \"exportTemplate\", Desc: \"导出模板\"},\n\t\t\t\tmodel.SysBaseMenuBtn{Name: \"exportExcel\", Desc: \"导出Excel\"},\n\t\t\t\tmodel.SysBaseMenuBtn{Name: \"importExcel\", Desc: \"导入Excel\"},\n\t\t\t)\n\t\t}\n\t}\n\tif err = tx.Create(&desired).Error; err != nil {\n\t\treturn fmt.Errorf(\"创建自动代码菜单 %s 失败: %w\", desired.Name, err)\n\t}\n\thistory.MenuID = desired.ID\n\treturn nil\n}\n\nfunc persistAutoCodeExportTemplate(tx *gorm.DB, info request.AutoCode, history *request.SysAutoHistoryCreate) error {\n\tif !info.HasExcel {\n\t\treturn nil\n\t}\n\tfields := make(map[string]string, len(info.Fields))\n\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)","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/auto_code_persistence.go#L96-L132","documentation":"Thrown by persistAutoCodeMenu when tx.Create(&desired) fails to insert the newly built SysBaseMenu row inside the auto-code creation transaction. The insert error is wrapped with the menu name so the developer knows which generated menu could not be persisted.","triggerScenarios":"Inserting the generated SysBaseMenu during auto-code Create fails: NOT NULL/unique constraint violation (duplicate menu name/path under same parent), field too long (e.g. component/name exceeding column size), or connection failure at INSERT time.","commonSituations":"Re-running code generation after a partially deleted menu leaves a conflicting name/path record; DB schema drift where new columns were added but not migrated; overly long StructName/Abbreviation producing a menu name past the column limit.","solutions":["Inspect the wrapped %w cause: unique constraint -> delete or rename the conflicting sys_base_menu row.","Ensure schema is up to date (AutoMigrate sys_base_menu) before regenerating.","Shorten StructName/Abbreviation so generated menu name/path fit column lengths.","Retry after fixing; the surrounding transaction rolls back partial writes."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"var cnt int64\ndb.Model(&system.SysBaseMenu{}).\n    Where(\"name = ?\", desiredName).Count(&cnt)\nif cnt > 0 { /* clean up or rename first */ }","typeGuard":null,"tryCatchPattern":"if err := createAutoCode(req); err != nil {\n    if strings.Contains(err.Error(), \"Duplicate entry\") || strings.Contains(err.Error(), \"UNIQUE\") {\n        removeStaleMenu(desiredName)\n        err = createAutoCode(req)\n    }\n}","preventionTips":["Delete both menu and export template when removing generated code.","Keep StructName/Abbreviation short to fit column limits.","Keep schema migrated to the current model version.","Rely on the transaction rollback; never patch rows manually mid-generation."],"tags":["gorm","insert","auto-code","menu"],"backgroundTag":"db-insert-constraint-violation","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}