{"record":{"id":"55cd928a1b72b49e","repo":"flipped-aurora/gin-vue-admin","slug":"s-55cd92","errorCode":null,"errorMessage":"%s表数据初始化失败!","messagePattern":"(.+?)表数据初始化失败!","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"server/source/system/authority.go","lineNumber":53,"sourceCode":"}\n\nfunc (i *initAuthority) InitializerName() string {\n\treturn sysModel.SysAuthority{}.TableName()\n}\n\nfunc (i *initAuthority) InitializeData(ctx context.Context) (context.Context, error) {\n\tdb, ok := ctx.Value(\"db\").(*gorm.DB)\n\tif !ok {\n\t\treturn ctx, system.ErrMissingDBContext\n\t}\n\tentities := []sysModel.SysAuthority{\n\t\t{AuthorityId: 888, AuthorityName: \"普通用户\", ParentId: utils.Pointer[uint](0), DefaultRouter: \"dashboard\"},\n\t\t{AuthorityId: 9528, AuthorityName: \"测试角色\", ParentId: utils.Pointer[uint](0), DefaultRouter: \"dashboard\"},\n\t\t{AuthorityId: 8881, AuthorityName: \"普通用户子角色\", ParentId: utils.Pointer[uint](888), DefaultRouter: \"dashboard\"},\n\t}\n\n\tif err := db.Create(&entities).Error; err != nil {\n\t\treturn ctx, errors.Wrapf(err, \"%s表数据初始化失败!\", sysModel.SysAuthority{}.TableName())\n\t}\n\n\tnext := context.WithValue(ctx, i.InitializerName(), entities)\n\treturn next, nil\n}\n\nfunc (i *initAuthority) 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(\"authority_id = ?\", \"8881\").\n\t\tFirst(&sysModel.SysAuthority{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据\n\t\treturn false\n\t}\n\treturn true\n}\n","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/source/system/authority.go#L35-L71","documentation":"The initAuthority initializer seeds sys_authorities with the default roles (888, 9528, 8881, and its parent 888). The bulk Create failed and was wrapped via errors.Wrapf with the table name. Downstream initializers (menus-authority, Casbin) depend on this data, so the whole init aborts.","triggerScenarios":"INSERT INTO sys_authorities fails during InitDB - duplicate AuthorityId rows from a previous init, stale table schema, lost connection, or missing INSERT privilege. Because authority_id is the primary key, re-running init on a seeded DB reliably trips this.","commonSituations":"Re-running initdb on an already-initialized database; upgrading gin-vue-admin over an old sys_authorities schema; DB account without write access; MySQL/PG outage mid-init.","solutions":["Look at the wrapped cause for the driver-level error (duplicate primary key 888, unknown column, access denied).","If re-initializing, drop the database or delete existing sys_authorities rows first.","Ensure AutoMigrate has brought sys_authorities to the current schema before seeding.","Verify the DB user has INSERT privileges and connectivity is stable.","Re-run InitDB; on success the roles are stored in the context for later initializers."],"exampleFix":"// before: duplicate primary key on re-init\n// Error 1062: Duplicate entry '888' for key 'PRIMARY'\n// after: clean the table before re-seeding\nDELETE FROM sys_authorities WHERE authority_id IN (888, 9528, 8881);\n// then re-run /init/initdb","handlingStrategy":"try-catch","validationCode":"// detect pre-existing authority rows before seeding\nvar n int64\ndb.Model(&sysModel.SysAuthority{}).Count(&n)\nif n > 0 { /* already seeded - skip or wipe first */ }","typeGuard":null,"tryCatchPattern":"if err := initdb(); err != nil {\n    if strings.Contains(err.Error(), \"sys_authorities\") {\n        log.Printf(\"authority seed failed: %v\", errors.Unwrap(err)) // duplicate 888?\n    }\n    return err\n}","preventionTips":["Never run initdb twice against the same database","Delete legacy sys_authorities rows (888/9528/8881) before re-seeding","Run AutoMigrate to upgrade old schemas first","Grant the init user INSERT on sys_authorities"],"tags":["database","gorm","seed-data","initialization"],"backgroundTag":"db-seed-insert-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}