{"record":{"id":"6b39e1e94b0c8142","repo":"flipped-aurora/gin-vue-admin","slug":"sys-users","errorCode":null,"errorMessage":"sys_users表数据初始化失败!","messagePattern":"sys_users表数据初始化失败!","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/source/system/user.go","lineNumber":79,"sourceCode":"\t\t\tPassword:    adminPassword,\n\t\t\tNickName:    \"Mr.奇淼\",\n\t\t\tHeaderImg:   \"https://qmplusimg.henrongyi.top/gva_header.jpg\",\n\t\t\tAuthorityId: 888,\n\t\t\tPhone:       \"17611111111\",\n\t\t\tEmail:       \"333333333@qq.com\",\n\t\t},\n\t\t{\n\t\t\tUUID:        uuid.New(),\n\t\t\tUsername:    \"a303176530\",\n\t\t\tPassword:    password,\n\t\t\tNickName:    \"用户1\",\n\t\t\tHeaderImg:   \"https://qmplusimg.henrongyi.top/1572075907logo.png\",\n\t\t\tAuthorityId: 9528,\n\t\t\tPhone:       \"17611111111\",\n\t\t\tEmail:       \"333333333@qq.com\"},\n\t}\n\tif err = db.Create(&entities).Error; err != nil {\n\t\treturn ctx, errors.Wrap(err, sysModel.SysUser{}.TableName()+\"表数据初始化失败!\")\n\t}\n\tnext = context.WithValue(ctx, i.InitializerName(), entities)\n\tauthorityEntities, ok := ctx.Value(new(initAuthority).InitializerName()).([]sysModel.SysAuthority)\n\tif !ok {\n\t\treturn next, errors.Wrap(system.ErrMissingDependentContext, \"创建 [用户-权限] 关联失败, 未找到权限表初始化数据\")\n\t}\n\tif err = db.Model(&entities[0]).Association(\"Authorities\").Replace(authorityEntities); err != nil {\n\t\treturn next, err\n\t}\n\tif err = db.Model(&entities[1]).Association(\"Authorities\").Replace(authorityEntities[:1]); err != nil {\n\t\treturn next, err\n\t}\n\treturn next, err\n}\n\nfunc (i *initUser) DataInserted(ctx context.Context) bool {\n\tdb, ok := ctx.Value(\"db\").(*gorm.DB)\n\tif !ok {","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/source/system/user.go#L61-L97","documentation":"Thrown by the user initializer (initUser.InitializeData) when inserting the default seed users (admin 9528 etc.) into sys_users fails. It wraps the GORM error with the table name. Note the function continues to attach Authorities; this specific error concerns only the initial user-row Create.","triggerScenarios":"db.Create(&entities) fails: sys_users not migrated, duplicate username/authority unique conflict from prior seeding, NOT NULL or column-type violation, password-hash column length exceeded, or DB connectivity loss.","commonSituations":"Running init twice on the same DB (user admin already exists → unique violation); partial seed from a crashed init left some user rows; schema drift after upgrade (new NOT NULL columns without defaults); DB in read-only mode.","solutions":["Inspect the wrapped GORM cause — duplicate key means users already exist and init can be skipped","AutoMigrate SysUser (and authority/user-association tables) before running the initializer","Guard with DataInserted(ctx) / the system init flag so seeding happens only on a fresh DB","Delete the conflicting sys_users rows (or recreate the DB) and re-run init","Verify DB credentials and that the connection is writable"],"exampleFix":"// before: second boot re-seeds and hits duplicate user\nctx, err = initUser{}.InitializeData(ctx)\n// after: only seed on fresh DB\nif !(initUser{}).DataInserted(ctx) {\n    if err := db.AutoMigrate(&sysModel.SysUser{}); err != nil { return err }\n    ctx, err = initUser{}.InitializeData(ctx)\n}","handlingStrategy":"validation","validationCode":"if !db.Migrator().HasTable(&sysModel.SysUser{}) {\n    if err := db.AutoMigrate(&sysModel.SysUser{}); err != nil { return err }\n}\nvar count int64\nif err := db.Model(&sysModel.SysUser{}).Count(&count).Error; err != nil { return err }\nif count > 0 { return nil } // users already seeded (e.g. admin 9528)","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 = initUser{}.InitializeData(ctx)\nif err != nil {\n    if strings.Contains(err.Error(), \"sys_users表数据初始化失败\") {\n        log.Printf(\"user seed failed, likely duplicate admin: cause=%v\", errors.Cause(err))\n    }\n    return err\n}","preventionTips":["Respect the system init flag — never re-run user seeding on an initialized DB","AutoMigrate user/authority tables before init","Check for an existing admin/username before inserting seed users","Verify DB credentials and writability at startup"],"tags":["gorm","database","user","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"}