flipped-aurora/gin-vue-admin · error

创建 [菜单-权限] 关联失败, 未找到权限表初始化数据

Error message

创建 [菜单-权限] 关联失败, 未找到权限表初始化数据

What it means

initMenuAuthority builds role-menu associations from data produced by earlier initializers. It reads the initAuthority result from the context and, if it is absent or not a []sysModel.SysAuthority, wraps ErrMissingDependentContext with this message. It means the authority initializer never ran or its result was lost before this step.

Source

Thrown at server/source/system/authorities_menus.go:42

func (i *initMenuAuthority) TableCreated(ctx context.Context) bool {
	return false // always replace
}

func (i *initMenuAuthority) InitializerName() string {
	return "sys_menu_authorities"
}

func (i *initMenuAuthority) InitializeData(ctx context.Context) (next context.Context, err error) {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return ctx, system.ErrMissingDBContext
	}

	initAuth := &initAuthority{}
	authorities, ok := ctx.Value(initAuth.InitializerName()).([]sysModel.SysAuthority)
	if !ok {
		return ctx, errors.Wrap(system.ErrMissingDependentContext, "创建 [菜单-权限] 关联失败, 未找到权限表初始化数据")
	}

	allMenus, ok := ctx.Value(new(initMenu).InitializerName()).([]sysModel.SysBaseMenu)
	if !ok {
		return next, errors.Wrap(errors.New(""), "创建 [菜单-权限] 关联失败, 未找到菜单表初始化数据")
	}
	next = ctx

	// 构建菜单ID映射,方便快速查找
	menuMap := make(map[uint]sysModel.SysBaseMenu)
	for _, menu := range allMenus {
		menuMap[menu.ID] = menu
	}

	// 为不同角色分配不同权限
	// 1. 超级管理员角色(888) - 拥有所有菜单权限
	if err = db.Model(&authorities[0]).Association("SysBaseMenus").Replace(allMenus); err != nil {
		return next, errors.Wrap(err, "为超级管理员分配菜单失败")

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Run the full initializer chain (or /init/initdb) rather than invoking initMenuAuthority alone.
  2. Ensure initAuthority runs before initMenuAuthority and check/propagate its returned error.
  3. Verify the context passed in is the one returned by the previous initializer (chain the next ctx).
  4. Check registerInitializers order matches dependencies: authority before authorities_menus.
  5. If the DB is already initialized, skip re-init; DataInserted should return true and this path not execute.

Example fix

// before: step called out of order
ctx, err := initMenuAuthority.InitializeData(ctx) // missing authority data
// after: run chain in dependency order
ctx, err = initAuthority.InitializeData(ctx)
if err != nil {
    return err
}
ctx, err = initMenuAuthority.InitializeData(ctx)
Defensive patterns

Strategy: validation

Validate before calling

// verify the dependent initializer output exists in ctx before calling
auths, ok := ctx.Value(new(initAuthority).InitializerName()).([]sysModel.SysAuthority)
if !ok || len(auths) == 0 {
    return errors.New("run initAuthority.InitializeData before menu-authority binding")
}

Type guard

func hasAuthorityContext(ctx context.Context) bool {
    auths, ok := ctx.Value(new(initAuthority).InitializerName()).([]sysModel.SysAuthority)
    return ok && len(auths) > 0
}

Try / catch

next, err := initMenuAuthority.InitializeData(ctx)
if errors.Is(err, system.ErrMissingDependentContext) {
    return fmt.Errorf("initializer chain broken - re-run full initdb: %w", err)
}

Prevention

When it happens

Trigger: Calling InitializeData for initMenuAuthority without the initializer chain having run initAuthority first - e.g. invoking an initializer out of order, a prior initializer aborted so its result was never stored via context.WithValue, or DataInserted checks were bypassed.

Common situations: Custom init code that calls initializers individually in the wrong order; a earlier initializer failed silently in a wrapper that swallowed the abort; modifying the init order in registerInitializers; partial init where authorities table creation was skipped.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/bc21b5dfaacf8d54. Report an issue: GitHub.