flipped-aurora/gin-vue-admin · error

sys_export_templates表数据初始化失败!

Error message

sys_export_templates表数据初始化失败!

What it means

Thrown by the sys_export_templates initializer (initExcelTemplate.InitializeData) when the bulk insert of the default export-template seed rows fails via db.Create(&entities). The error wraps the underlying GORM error and names sys_export_templates so the failing table is identifiable.

Source

Thrown at server/source/system/excel_template.go:60

	if !ok {
		return ctx, system.ErrMissingDBContext
	}

	entities := []sysModel.SysExportTemplate{
		{
			Name:       "api",
			TableName:  "sys_apis",
			TemplateID: "api",
			TemplateInfo: `{
"path":"路径",
"method":"方法(大写)",
"description":"方法介绍",
"api_group":"方法分组"
}`,
		},
	}
	if err := db.Create(&entities).Error; err != nil {
		return ctx, errors.Wrap(err, "sys_export_templates"+"表数据初始化失败!")
	}
	next := context.WithValue(ctx, i.InitializerName(), entities)
	return next, nil
}

func (i *initExcelTemplate) DataInserted(ctx context.Context) bool {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return false
	}
	if errors.Is(db.First(&sysModel.SysExportTemplate{}).Error, gorm.ErrRecordNotFound) {
		return false
	}
	return true
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Read the wrapped cause for the raw SQL error and address it directly
  2. Ensure db.AutoMigrate(&excelModel.SysExportTemplate{}) runs before initialization
  3. Truncate sys_export_templates and re-run the initializer for a clean seed
  4. Verify DB credentials/connectivity and that the database is writable
  5. Check SQL mode / column constraints against the seed row fields

Example fix

// before: assumes table exists
ctx, err = initExcelTemplate{}.InitializeData(ctx)
// after: guard with migration and inserted check
if err := db.Set("gorm:table_options", "").AutoMigrate(&excelModel.SysExportTemplate{}); err != nil { return err }
if !(initExcelTemplate{}).DataInserted(ctx) {
    ctx, err = initExcelTemplate{}.InitializeData(ctx)
}
Defensive patterns

Strategy: validation

Validate before calling

if !db.Migrator().HasTable(&excelModel.SysExportTemplate{}) {
    if err := db.AutoMigrate(&excelModel.SysExportTemplate{}); err != nil { return err }
}
var count int64
if err := db.Model(&excelModel.SysExportTemplate{}).Count(&count).Error; err != nil { return err }
if count > 0 { return nil }

Type guard

func hasDB(ctx context.Context) (*gorm.DB, bool) {
    db, ok := ctx.Value("db").(*gorm.DB)
    return db, ok && db != nil
}

Try / catch

ctx, err = initExcelTemplate{}.InitializeData(ctx)
if err != nil {
    log.Printf("export template seed failed: %v", errors.Cause(err))
    return err
}

Prevention

When it happens

Trigger: db.Create(&entities) returns an error: table sys_export_templates not migrated, duplicate key from prior partial seeding, NOT NULL/constraint violation in a seed row, or a DB connection failure during the insert.

Common situations: Running init against an existing DB where templates were already inserted (unique/duplicate conflict); template JSON column content rejected by strict SQL modes; database read-only or quota exhausted; dialect mismatch after switching mysql→pgsql.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/1310b71903100748. Report an issue: GitHub.