flipped-aurora/gin-vue-admin · error

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

Error message

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

What it means

During system initialization, the authorities_menus initializer reads its prerequisites from the initializer context: the authorities seed data and the menus seed data (keyed by their InitializerName). When the menus value is absent or of the wrong type, it wraps a new error as "创建 [菜单-权限] 关联失败, 未找到菜单表初始化数据" via errors.Wrap.

Source

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

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, "为超级管理员分配菜单失败")
	}

	// 2. 普通用户角色(8881) - 仅拥有基础功能菜单
	// 仅选择部分父级菜单及其子菜单
	var menu8881 []sysModel.SysBaseMenu

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Ensure initMenu runs before the authorities_menus initializer in the registration/order list (system/init/*.go).
  2. Check that the menu initializer completed successfully (its data must be in ctx under new(initMenu).InitializerName()).
  3. If you customized initializers, keep the ctx key type and name identical to the built-in initMenu.
  4. Re-run full system initialization rather than a single initializer.

Example fix

// before
// initOrder: [initDB, initAuthority, initAuthoritiesMenus] // initMenu missing
// after
// initOrder: [initDB, initMenu, initAuthority, initAuthoritiesMenus]
Defensive patterns

Strategy: validation

Validate before calling

// before running authorities_menus init, verify prerequisite in ctx
if _, ok := ctx.Value(new(initMenu).InitializerName()).([]sysModel.SysBaseMenu); !ok {
  return errors.New("run initMenu initializer first")
}

Type guard

menus, ok := ctx.Value(new(initMenu).InitializerName()).([]sysModel.SysBaseMenu)
if !ok { return fmt.Errorf("menu init data missing") }

Try / catch

if err := initAuthoritiesMenus.InitializeData(ctx); err != nil {
  if strings.Contains(err.Error(), "未找到菜单表初始化数据") {
    logger.Error("initializer order wrong: initMenu must run before authorities_menus")
  }
  return err
}

Prevention

When it happens

Trigger: Running the authorities_menus InitializeData (authorities_menus.go:47) when the initMenu initializer did not run first, was disabled in the registration order, or its context value was overwritten/removed by another initializer.

Common situations: Custom init order in the initializer registry that skips initMenu; partial initialization after a failed earlier step; developers writing custom initializers that clobber shared context keys; running only selected initializers.

Related errors


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