{"record":{"id":"58b90f265df3bd0e","repo":"flipped-aurora/gin-vue-admin","slug":"sys-dictionaries","errorCode":null,"errorMessage":"sys_dictionaries表数据初始化失败!","messagePattern":"sys_dictionaries表数据初始化失败!","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/source/system/dictionary.go","lineNumber":56,"sourceCode":"}\n\nfunc (i *initDict) InitializeData(ctx context.Context) (next context.Context, err error) {\n\tdb, ok := ctx.Value(\"db\").(*gorm.DB)\n\tif !ok {\n\t\treturn ctx, system.ErrMissingDBContext\n\t}\n\tTrue := true\n\tentities := []sysModel.SysDictionary{\n\t\t{Name: \"性别\", Type: \"gender\", Status: &True, Desc: \"性别字典\"},\n\t\t{Name: \"数据库int类型\", Type: \"int\", Status: &True, Desc: \"int类型对应的数据库类型\"},\n\t\t{Name: \"数据库时间日期类型\", Type: \"time.Time\", Status: &True, Desc: \"数据库时间日期类型\"},\n\t\t{Name: \"数据库浮点型\", Type: \"float64\", Status: &True, Desc: \"数据库浮点型\"},\n\t\t{Name: \"数据库字符串\", Type: \"string\", Status: &True, Desc: \"数据库字符串\"},\n\t\t{Name: \"数据库bool类型\", Type: \"bool\", Status: &True, Desc: \"数据库bool类型\"},\n\t}\n\n\tif err = db.Create(&entities).Error; err != nil {\n\t\treturn ctx, errors.Wrap(err, sysModel.SysDictionary{}.TableName()+\"表数据初始化失败!\")\n\t}\n\tnext = context.WithValue(ctx, i.InitializerName(), entities)\n\treturn next, nil\n}\n\nfunc (i *initDict) DataInserted(ctx context.Context) bool {\n\tdb, ok := ctx.Value(\"db\").(*gorm.DB)\n\tif !ok {\n\t\treturn false\n\t}\n\tif errors.Is(db.Where(\"type = ?\", \"bool\").First(&sysModel.SysDictionary{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据\n\t\treturn false\n\t}\n\treturn true\n}\n","sourceCodeStart":38,"sourceCodeEnd":72,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/source/system/dictionary.go#L38-L72","documentation":"This error is thrown by the sys_dictionaries system-table initializer (initDict.InitializeData) when the bulk GORM insert of the seed dictionary rows (数据库浮点型/字符串/bool类型 etc.) fails. It wraps the underlying *gorm.DB error with pkg/errors.Wrap so the failing table is named. It occurs during system initialization (first boot / RegisterInit).","triggerScenarios":"Calling InitializeData with the *gorm.DB stored in ctx under key \"db\" when db.Create(&entities) returns a non-nil error — e.g. the sys_dictionaries table does not exist (AutoMigrate not run), a duplicate primary key/unique conflict from partial prior seeding, or a DB connection failure mid-insert.","commonSituations":"Running the initializer against a database where the table was partially created by an earlier failed init; connecting to a DB with wrong credentials or a dropped connection; switching DB dialects (mysql/pgsql/sqlite) where a column type in the seed model is unsupported; running init twice without truncation causing duplicate-key errors.","solutions":["Check the wrapped cause (errors.Cause / %v of err) to see the raw GORM/SQL error and fix the root DB issue","Ensure the table exists first: run the AutoMigrate step for SysDictionary before InitializeData","Drop and recreate the database (or TRUNCATE sys_dictionaries) so the seed insert starts from a clean state","Verify DB connectivity/credentials in config (host, port, user, password, dbname) and that the server is reachable","Confirm the dialect-specific model columns are compatible with your configured DB (e.g. pgsql vs mysql)"],"exampleFix":"// before: init runs before migration, table missing\ncount, _ := db.Count(...) // assume exists\ninitDict{}.InitializeData(ctx)\n// after: migrate then init, and only if not already inserted\nif !db.Migrator().HasTable(&sysModel.SysDictionary{}) {\n    _ = db.AutoMigrate(&sysModel.SysDictionary{})\n}\nif !(initDict{}).DataInserted(ctx) {\n    ctx, err = initDict{}.InitializeData(ctx)\n}","handlingStrategy":"validation","validationCode":"if !db.Migrator().HasTable(&sysModel.SysDictionary{}) {\n    if err := db.AutoMigrate(&sysModel.SysDictionary{}); err != nil { return err }\n}\nvar count int64\nif err := db.Model(&sysModel.SysDictionary{}).Count(&count).Error; err != nil { return err }\nif count > 0 { return nil } // already seeded, skip","typeGuard":"func hasDB(ctx context.Context) (*gorm.DB, bool) {\n    db, ok := ctx.Value(\"db\").(*gorm.DB)\n    return db, ok && db != nil\n}","tryCatchPattern":"ctx, err := initDict{}.InitializeData(ctx)\nif err != nil {\n    log.Printf(\"dict seed failed: %v (cause: %v)\", err, errors.Cause(err))\n    return fmt.Errorf(\"初始化字典失败: %w\", err)\n}","preventionTips":["Always run AutoMigrate for all seed models before any initializer","Call DataInserted(ctx) before seeding to make init idempotent","Initialize against a fresh/empty database and run init exactly once","Check DB connectivity and credentials at startup before the init chain","Log errors.Cause(err) to expose the raw GORM/SQL driver error"],"tags":["gorm","database","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"}