flipped-aurora/gin-vue-admin · error
创建自动代码导出模板 %s 失败: %w
Error message
创建自动代码导出模板 %s 失败: %w
What it means
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).
Source
Thrown at server/service/system/auto_code_persistence.go:143
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)
}
name := info.Package + "_" + info.StructName
entity := model.SysExportTemplate{
DBName: info.BusinessDB,
Name: name,
TableName: info.TableName,
TemplateID: name,
TemplateInfo: string(templateInfo),
}
if err = tx.Create(&entity).Error; err != nil {
return fmt.Errorf("创建自动代码导出模板 %s 失败: %w", name, err)
}
history.ExportTemplateID = entity.ID
return nil
}
func autoCodeIdentityExists(ctx context.Context, db *gorm.DB, info request.AutoCode) (bool, error) {
if db == nil {
return false, errors.New("自动代码数据库未初始化")
}
var count int64
err := db.WithContext(ctx).Model(&model.SysAutoCodeHistory{}).
Where(
"business_db = ? AND (struct_name = ? OR abbreviation = ?) AND package = ? AND flag = ?",
info.BusinessDB,
info.StructName,
info.Abbreviation,
info.Package,
0,View on GitHub (pinned to 3136500ef3)
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.
Defensive patterns
Strategy: try-catch
Validate before calling
var cnt int64
db.Model(&system.SysExportTemplate{}).
Where("template_id = ?", pkg+"_"+structName).Count(&cnt)
if cnt > 0 { /* remove stale template first */ } Try / catch
if err := createAutoCode(req); err != nil {
if strings.Contains(err.Error(), "Duplicate entry") {
db.Where("template_id = ?", name).Delete(&system.SysExportTemplate{})
err = createAutoCode(req)
}
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/0e50e4155be05544.
Report an issue: GitHub.