flipped-aurora/gin-vue-admin · error

为测试角色分配菜单失败

Error message

为测试角色分配菜单失败

What it means

initMenuAuthority assigns the systemTools/example menus to the test role (authorities[2], ID 9528) via Association Replace. The GORM operation failed and was wrapped with this message, so role 9528 ends up without its menu bindings and the init chain aborts.

Source

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

		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
		}

		if menu.ParentId > 0 && (parentName == "systemTools" || parentName == "example") {
			menu9528 = append(menu9528, menu)
		}
	}

	if err = db.Model(&authorities[2]).Association("SysBaseMenus").Replace(menu9528); err != nil {
		return next, errors.Wrap(err, "为测试角色分配菜单失败")
	}

	return next, nil
}

func (i *initMenuAuthority) DataInserted(ctx context.Context) bool {
	db, ok := ctx.Value("db").(*gorm.DB)
	if !ok {
		return false
	}
	auth := &sysModel.SysAuthority{}
	if ret := db.Model(auth).
		Where("authority_id = ?", 9528).Preload("SysBaseMenus").Find(auth); ret != nil {
		if ret.Error != nil {
			return false
		}
		return len(auth.SysBaseMenus) > 0
	}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Check the wrapped driver error for the root cause.
  2. Drop and re-create the database, then run /init/initdb so all seeds and associations are written atomically.
  3. Verify the join table schema matches the current release (AutoMigrate).
  4. Confirm the authorities context contains the full seeded role list so authorities[2] is 9528.
  5. Grant write privileges on sys_authorities_menus and re-run init.
Defensive patterns

Strategy: try-catch

Validate before calling

if len(authorities) < 3 { return errors.New("authority seed incomplete for role 9528") }
if !db.Migrator().HasTable("sys_authorities_menus") { return errors.New("join table missing") }

Try / catch

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

Prevention

When it happens

Trigger: Replace on sys_authorities_menus for authority 9528 fails - join table missing or schema-stale, FK violation from missing menu/authority rows, transient DB error, or privilege denial on the join table.

Common situations: Partially initialized database from a failed earlier run; upgrade against legacy schema; DB user lacking INSERT on sys_authorities_menus; connection pool exhaustion mid-init on large seed sets.

Related errors


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