flipped-aurora/gin-vue-admin · error
sys_dictionary_details表数据初始化失败!
Error message
sys_dictionary_details表数据初始化失败!
What it means
Thrown by the sys_dictionary_details initializer when GORM's Association Replace of the seeded detail rows onto each dictionary fails (db.Model(&dict).Association("SysDictionaryDetails").Replace). It wraps the raw GORM error and names the table. Replace first deletes existing associations then inserts the new details, so it fails on either phase.
Source
Thrown at server/source/system/dictionary_detail.go:104
{Label: "varchar", Value: "1", Status: &True, Extend: "mysql", Sort: 1},
{Label: "tinyblob", Value: "2", Status: &True, Extend: "mysql", Sort: 2},
{Label: "tinytext", Value: "3", Status: &True, Extend: "mysql", Sort: 3},
{Label: "text", Value: "4", Status: &True, Extend: "mysql", Sort: 4},
{Label: "blob", Value: "5", Status: &True, Extend: "mysql", Sort: 5},
{Label: "mediumblob", Value: "6", Status: &True, Extend: "mysql", Sort: 6},
{Label: "mediumtext", Value: "7", Status: &True, Extend: "mysql", Sort: 7},
{Label: "longblob", Value: "8", Status: &True, Extend: "mysql", Sort: 8},
{Label: "longtext", Value: "9", Status: &True, Extend: "mysql", Sort: 9},
}
dicts[5].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
{Label: "tinyint", Value: "1", Extend: "mysql", Status: &True},
{Label: "bool", Value: "2", Extend: "pgsql", Status: &True},
}
for _, dict := range dicts {
if err := db.Model(&dict).Association("SysDictionaryDetails").
Replace(dict.SysDictionaryDetails); err != nil {
return ctx, errors.Wrap(err, sysModel.SysDictionaryDetail{}.TableName()+"表数据初始化失败!")
}
}
return ctx, nil
}
func (i *initDictDetail) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
var dict sysModel.SysDictionary
if err := db.Preload("SysDictionaryDetails").
First(&dict, &sysModel.SysDictionary{Name: "数据库bool类型"}).Error; err != nil {
return false
}
return len(dict.SysDictionaryDetails) > 0 && dict.SysDictionaryDetails[0].Label == "tinyint"
}
View on GitHub (pinned to 3136500ef3)
Solutions
- Inspect the wrapped GORM error to identify whether the DELETE or INSERT phase failed
- AutoMigrate SysDictionary and SysDictionaryDetail (including the association FK) before running this initializer
- Truncate sys_dictionary_details so Replace starts from a consistent state
- Re-run initDict first so the dicts slice in ctx holds freshly created dictionaries with valid IDs
- Check FK/dialect compatibility (mysql vs pgsql) for the seeded Extend/Value columns
Example fix
// before: details table missing FK, Replace fails
ctx, err = initDictDetail{}.InitializeData(ctx)
// after: migrate both models first
_ = db.AutoMigrate(&sysModel.SysDictionary{}, &sysModel.SysDictionaryDetail{})
ctx, err = initDictDetail{}.InitializeData(ctx) Defensive patterns
Strategy: validation
Validate before calling
if !db.Migrator().HasTable(&sysModel.SysDictionaryDetail{}) {
if err := db.AutoMigrate(&sysModel.SysDictionary{}, &sysModel.SysDictionaryDetail{}); err != nil { return err }
}
dicts, ok := ctx.Value(new(initDict).InitializerName()).([]sysModel.SysDictionary)
if !ok || len(dicts) == 0 { return errors.New("missing dictionary seed data") } Type guard
func associationReady(db *gorm.DB, dict *sysModel.SysDictionary) bool {
return db != nil && dict != nil && dict.ID != 0 &&
db.Migrator().HasTable(&sysModel.SysDictionaryDetail{})
} Try / catch
ctx, err = initDictDetail{}.InitializeData(ctx)
if err != nil {
log.Printf("detail association replace failed: cause=%v", errors.Cause(err))
return err
} Prevention
- AutoMigrate both dictionary models before the detail initializer
- Re-run initDict first so association IDs are valid
- Make seeding idempotent with DataInserted checks to avoid orphaned rows
- Verify FK constraints and dialect compatibility (mysql vs pgsql) before init
When it happens
Trigger: Running InitializeData when the sys_dictionary_details table is missing (no AutoMigrate), when a foreign-key constraint on sys_dictionary_details.dictionary_id is violated, or when the dictionary rows referenced by dicts were deleted/changed since initDict ran.
Common situations: Partial seeding from a previously failed run left orphaned/conflicting detail rows; DB enforces FK constraints (pgsql/mysql InnoDB) and parent dictionary IDs are stale; dialect-specific column type mismatch (e.g. tinyint label on pgsql) rejected at insert; connection dropped mid-loop over dicts.
Related errors
- media.FileUploadAndDownload表数据初始化失败!
- sys_apis表数据初始化失败!
- sys_ignore_apis表数据初始化失败!
- 为超级管理员分配菜单失败
- 为普通用户分配菜单失败
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/44f9bc0d87d76112.
Report an issue: GitHub.