{"record":{"id":"44f9bc0d87d76112","repo":"flipped-aurora/gin-vue-admin","slug":"sys-dictionary-details","errorCode":null,"errorMessage":"sys_dictionary_details表数据初始化失败!","messagePattern":"sys_dictionary_details表数据初始化失败!","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/source/system/dictionary_detail.go","lineNumber":104,"sourceCode":"\t\t{Label: \"varchar\", Value: \"1\", Status: &True, Extend: \"mysql\", Sort: 1},\n\t\t{Label: \"tinyblob\", Value: \"2\", Status: &True, Extend: \"mysql\", Sort: 2},\n\t\t{Label: \"tinytext\", Value: \"3\", Status: &True, Extend: \"mysql\", Sort: 3},\n\t\t{Label: \"text\", Value: \"4\", Status: &True, Extend: \"mysql\", Sort: 4},\n\t\t{Label: \"blob\", Value: \"5\", Status: &True, Extend: \"mysql\", Sort: 5},\n\t\t{Label: \"mediumblob\", Value: \"6\", Status: &True, Extend: \"mysql\", Sort: 6},\n\t\t{Label: \"mediumtext\", Value: \"7\", Status: &True, Extend: \"mysql\", Sort: 7},\n\t\t{Label: \"longblob\", Value: \"8\", Status: &True, Extend: \"mysql\", Sort: 8},\n\t\t{Label: \"longtext\", Value: \"9\", Status: &True, Extend: \"mysql\", Sort: 9},\n\t}\n\n\tdicts[5].SysDictionaryDetails = []sysModel.SysDictionaryDetail{\n\t\t{Label: \"tinyint\", Value: \"1\", Extend: \"mysql\", Status: &True},\n\t\t{Label: \"bool\", Value: \"2\", Extend: \"pgsql\", Status: &True},\n\t}\n\tfor _, dict := range dicts {\n\t\tif err := db.Model(&dict).Association(\"SysDictionaryDetails\").\n\t\t\tReplace(dict.SysDictionaryDetails); err != nil {\n\t\t\treturn ctx, errors.Wrap(err, sysModel.SysDictionaryDetail{}.TableName()+\"表数据初始化失败!\")\n\t\t}\n\t}\n\treturn ctx, nil\n}\n\nfunc (i *initDictDetail) DataInserted(ctx context.Context) bool {\n\tdb, ok := ctx.Value(\"db\").(*gorm.DB)\n\tif !ok {\n\t\treturn false\n\t}\n\tvar dict sysModel.SysDictionary\n\tif err := db.Preload(\"SysDictionaryDetails\").\n\t\tFirst(&dict, &sysModel.SysDictionary{Name: \"数据库bool类型\"}).Error; err != nil {\n\t\treturn false\n\t}\n\treturn len(dict.SysDictionaryDetails) > 0 && dict.SysDictionaryDetails[0].Label == \"tinyint\"\n}\n","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/source/system/dictionary_detail.go#L86-L122","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before: details table missing FK, Replace fails\nctx, err = initDictDetail{}.InitializeData(ctx)\n// after: migrate both models first\n_ = db.AutoMigrate(&sysModel.SysDictionary{}, &sysModel.SysDictionaryDetail{})\nctx, err = initDictDetail{}.InitializeData(ctx)","handlingStrategy":"validation","validationCode":"if !db.Migrator().HasTable(&sysModel.SysDictionaryDetail{}) {\n    if err := db.AutoMigrate(&sysModel.SysDictionary{}, &sysModel.SysDictionaryDetail{}); err != nil { return err }\n}\ndicts, ok := ctx.Value(new(initDict).InitializerName()).([]sysModel.SysDictionary)\nif !ok || len(dicts) == 0 { return errors.New(\"missing dictionary seed data\") }","typeGuard":"func associationReady(db *gorm.DB, dict *sysModel.SysDictionary) bool {\n    return db != nil && dict != nil && dict.ID != 0 &&\n        db.Migrator().HasTable(&sysModel.SysDictionaryDetail{})\n}","tryCatchPattern":"ctx, err = initDictDetail{}.InitializeData(ctx)\nif err != nil {\n    log.Printf(\"detail association replace failed: cause=%v\", errors.Cause(err))\n    return err\n}","preventionTips":["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"],"tags":["gorm","association","seed-data","initialization"],"backgroundTag":"seed-data-insert-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}