flipped-aurora/gin-vue-admin · error
创建 [用户-权限] 关联失败, 未找到权限表初始化数据
Error message
创建 [用户-权限] 关联失败, 未找到权限表初始化数据
What it means
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.
Source
Thrown at server/source/system/user.go:84
Email: "333333333@qq.com",
},
{
UUID: uuid.New(),
Username: "a303176530",
Password: password,
NickName: "用户1",
HeaderImg: "https://qmplusimg.henrongyi.top/1572075907logo.png",
AuthorityId: 9528,
Phone: "17611111111",
Email: "333333333@qq.com"},
}
if err = db.Create(&entities).Error; err != nil {
return ctx, errors.Wrap(err, sysModel.SysUser{}.TableName()+"表数据初始化失败!")
}
next = context.WithValue(ctx, i.InitializerName(), entities)
authorityEntities, ok := ctx.Value(new(initAuthority).InitializerName()).([]sysModel.SysAuthority)
if !ok {
return next, errors.Wrap(system.ErrMissingDependentContext, "创建 [用户-权限] 关联失败, 未找到权限表初始化数据")
}
if err = db.Model(&entities[0]).Association("Authorities").Replace(authorityEntities); err != nil {
return next, err
}
if err = db.Model(&entities[1]).Association("Authorities").Replace(authorityEntities[:1]); err != nil {
return next, err
}
return next, err
}
func (i *initUser) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
var record sysModel.SysUser
if errors.Is(db.Where("username = ?", "a303176530").
Preload("Authorities").First(&record).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据View on GitHub (pinned to 3136500ef3)
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{...})
Example fix
// before: user init on a context missing authority data
ctx, err = initUser{}.InitializeData(context.WithValue(ctx, "db", db))
// after: run dependents in order on the shared context
ctx, err = initAuthority{}.InitializeData(ctx)
if err != nil { return err }
ctx, err = initUser{}.InitializeData(ctx) Defensive patterns
Strategy: type-guard
Validate before calling
auths, ok := ctx.Value(new(initAuthority).InitializerName()).([]sysModel.SysAuthority)
if !ok || len(auths) == 0 {
return errors.New("run initAuthority before initUser")
} Type guard
func authoritiesInCtx(ctx context.Context) ([]sysModel.SysAuthority, bool) {
auths, ok := ctx.Value(new(initAuthority).InitializerName()).([]sysModel.SysAuthority)
return auths, ok && len(auths) > 0
} Try / catch
ctx, err = initUser{}.InitializeData(ctx)
if errors.Is(errors.Cause(err), system.ErrMissingDependentContext) {
return fmt.Errorf("初始化顺序错误, 请先初始化权限表: %w", err)
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- 未找到 %s 表初始化数据
- 创建 [菜单-权限] 关联失败, 未找到权限表初始化数据
- 数据库未初始化
- 创建 [菜单-权限] 关联失败, 未找到菜单表初始化数据
- media.FileUploadAndDownload表数据初始化失败!
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/91c8e6a52dfe1aa6.
Report an issue: GitHub.