flipped-aurora/gin-vue-admin · error

为普通用户分配菜单失败

Error message

为普通用户分配菜单失败

What it means

initMenuAuthority assigns the basic menu set to the normal-user role (authorities[1], ID 8881) via Association Replace on SysBaseMenus. The GORM operation failed and was wrapped with this message, leaving role 8881 without menu bindings.

Solutions

  1. Inspect the wrapped cause (unknown column, FK failure, access denied).
  2. Re-run a clean InitDB on an empty database so authority/menu seeds and join rows stay consistent.
  3. Confirm AutoMigrate created sys_authorities_menus with the current version's schema.
  4. Ensure initAuthority produced all 4 seeded roles so authorities[1] is the expected 8881 role.
  5. Verify DB write permissions and retry init.
Defensive patterns

Strategy: try-catch

Validate before calling

// guard index and table existence before Replace
if len(authorities) < 2 { return errors.New("authority seed incomplete") }
if !db.Migrator().HasTable("sys_authorities_menus") { return errors.New("join table missing") }

Try / catch

if err := db.Model(&authorities[1]).Association("SysBaseMenus").Replace(menu8881); err != nil {
    return fmt.Errorf("role 8881 menu binding failed: %w", err)
}

Prevention

When it happens

Trigger: Replace on the sys_authorities_menus join table fails for authority 8881 - missing/stale join table schema, FK violation because seeded menus or the authority row are absent, connection failure, or write privilege denied.

Common situations: Re-init on a DB where sys_authorities were seeded differently (fewer than 3 roles, so authorities[1] is the wrong row); interrupted previous init leaving partial join rows; older schema after upgrading gin-vue-admin.

Related errors


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

Appendix: source

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

	// 为不同角色分配不同权限
	// 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

	// 添加仪表盘、关于我们和个人信息菜单
	for _, menu := range allMenus {
		if menu.ParentId == 0 && (menu.Name == "dashboard" || menu.Name == "about" || menu.Name == "person" || menu.Name == "state") {
			menu8881 = append(menu8881, menu)
		}
	}

	if err = db.Model(&authorities[1]).Association("SysBaseMenus").Replace(menu8881); err != nil {
		return next, errors.Wrap(err, "为普通用户分配菜单失败")
	}

	// 3. 测试角色(9528) - 拥有部分菜单权限
	var menu9528 []sysModel.SysBaseMenu

	// 添加所有父级菜单
	for _, menu := range allMenus {
		if menu.ParentId == 0 {
			menu9528 = append(menu9528, menu)
		}
	}

	// 添加部分子菜单 - 系统工具、示例文件等模块的子菜单
	for _, menu := range allMenus {
		parentName := ""
		if menu.ParentId > 0 && menuMap[menu.ParentId].Name != "" {
			parentName = menuMap[menu.ParentId].Name
		}

View on GitHub (pinned to 3136500ef3)