{"record":{"id":"91c8e6a52dfe1aa6","repo":"flipped-aurora/gin-vue-admin","slug":"error-91c8e6","errorCode":null,"errorMessage":"创建 [用户-权限] 关联失败, 未找到权限表初始化数据","messagePattern":"创建 \\[用户-权限\\] 关联失败, 未找到权限表初始化数据","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/source/system/user.go","lineNumber":84,"sourceCode":"\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 {\n\t\treturn false\n\t}\n\tvar record sysModel.SysUser\n\tif errors.Is(db.Where(\"username = ?\", \"a303176530\").\n\t\tPreload(\"Authorities\").First(&record).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/source/system/user.go#L66-L102","documentation":"Thrown by the user initializer when the shared init context lacks the SysAuthority seed data created by initAuthority (looked up via new(initAuthority).InitializerName()). The sentinel is system.ErrMissingDependentContext; the users were created but their Authorities association cannot be built without the authority rows from the earlier initializer.","triggerScenarios":"Calling initUser.InitializeData with a ctx that never passed through initAuthority.InitializeData, or the authority initializer failed/stored a non-[]sysModel.SysAuthority value, so the context type assertion fails.","commonSituations":"Reordering registered initializers so user init runs before authority init; invoking the user initializer standalone (tests, scripts) with a ctx containing only \"db\"; an earlier authority-init error ignored, leaving its context value unset.","solutions":["Ensure initAuthority runs before initUser in the registration/execution order and that its error is checked","Thread the same ctx through all initializers instead of creating a fresh one for the user initializer","Fix the underlying authority-init failure (see related sys_authorities errors) then re-run the chain","In tests/custom flows, seed ctx manually: context.WithValue(ctx, new(initAuthority).InitializerName(), []sysModel.SysAuthority{...})"],"exampleFix":"// before: user init on a context missing authority data\nctx, err = initUser{}.InitializeData(context.WithValue(ctx, \"db\", db))\n// after: run dependents in order on the shared context\nctx, err = initAuthority{}.InitializeData(ctx)\nif err != nil { return err }\nctx, err = initUser{}.InitializeData(ctx)","handlingStrategy":"type-guard","validationCode":"auths, ok := ctx.Value(new(initAuthority).InitializerName()).([]sysModel.SysAuthority)\nif !ok || len(auths) == 0 {\n    return errors.New(\"run initAuthority before initUser\")\n}","typeGuard":"func authoritiesInCtx(ctx context.Context) ([]sysModel.SysAuthority, bool) {\n    auths, ok := ctx.Value(new(initAuthority).InitializerName()).([]sysModel.SysAuthority)\n    return auths, ok && len(auths) > 0\n}","tryCatchPattern":"ctx, err = initUser{}.InitializeData(ctx)\nif errors.Is(errors.Cause(err), system.ErrMissingDependentContext) {\n    return fmt.Errorf(\"初始化顺序错误, 请先初始化权限表: %w\", err)\n}","preventionTips":["Register initAuthority before initUser and never reorder initializers","Always check the previous initializer's error before running dependents","Pass one shared ctx through the whole init chain","Add a test asserting the full chain order end-to-end"],"tags":["gorm","initialization","dependency-order","context","authority"],"backgroundTag":"missing-dependent-init-context","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}