flipped-aurora/gin-vue-admin · error
创建自动代码菜单 %s 失败: %w
Error message
创建自动代码菜单 %s 失败: %w
What it means
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.
Source
Thrown at server/service/system/auto_code_persistence.go:114
if info.AutoCreateBtnAuth && !info.OnlyTemplate {
desired.MenuBtn = []model.SysBaseMenuBtn{
{Name: "add", Desc: "新增"},
{Name: "batchDelete", Desc: "批量删除"},
{Name: "delete", Desc: "删除"},
{Name: "edit", Desc: "编辑"},
{Name: "info", Desc: "详情"},
}
if info.HasExcel {
desired.MenuBtn = append(desired.MenuBtn,
model.SysBaseMenuBtn{Name: "exportTemplate", Desc: "导出模板"},
model.SysBaseMenuBtn{Name: "exportExcel", Desc: "导出Excel"},
model.SysBaseMenuBtn{Name: "importExcel", Desc: "导入Excel"},
)
}
}
if err = tx.Create(&desired).Error; err != nil {
return fmt.Errorf("创建自动代码菜单 %s 失败: %w", desired.Name, err)
}
history.MenuID = desired.ID
return nil
}
func persistAutoCodeExportTemplate(tx *gorm.DB, info request.AutoCode, history *request.SysAutoHistoryCreate) error {
if !info.HasExcel {
return nil
}
fields := make(map[string]string, len(info.Fields))
for _, field := range info.Fields {
if field != nil && field.Excel {
fields[field.ColumnName] = field.FieldDesc
}
}
templateInfo, err := json.Marshal(fields)
if err != nil {
return fmt.Errorf("序列化自动代码导出模板失败: %w", err)View on GitHub (pinned to 3136500ef3)
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.
Defensive patterns
Strategy: try-catch
Validate before calling
var cnt int64
db.Model(&system.SysBaseMenu{}).
Where("name = ?", desiredName).Count(&cnt)
if cnt > 0 { /* clean up or rename first */ } Try / catch
if err := createAutoCode(req); err != nil {
if strings.Contains(err.Error(), "Duplicate entry") || strings.Contains(err.Error(), "UNIQUE") {
removeStaleMenu(desiredName)
err = createAutoCode(req)
}
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- 查询自动代码菜单 %s 失败: %w
- 创建自动代码导出模板 %s 失败: %w
- %w: name=%s, 已存在 path=%s component=%s, 期望 path=%s component=
- 查询自动代码重复记录失败: %w
- 同步失败!
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/18bea04273c0d6b8.
Report an issue: GitHub.