elunez/eladmin · warning · BadRequestException
所选角色存在用户关联,请解除关联再试!
Error message
所选角色存在用户关联,请解除关联再试!
What it means
Thrown by RoleServiceImpl.verification before role deletion: if any user still has one of the selected roles (userRepository.countByRoles > 0), deletion is blocked. This keeps users from ending up with dangling role references. Surfaced as 400 Bad Request.
Source
Thrown at eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java:213
@Override
public void download(List<RoleDto> roles, HttpServletResponse response) throws IOException {
List<Map<String, Object>> list = new ArrayList<>();
for (RoleDto role : roles) {
Map<String, Object> map = new LinkedHashMap<>();
map.put("角色名称", role.getName());
map.put("角色级别", role.getLevel());
map.put("描述", role.getDescription());
map.put("创建日期", role.getCreateTime());
list.add(map);
}
FileUtil.downloadExcel(list, response);
}
@Override
public void verification(Set<Long> ids) {
if (userRepository.countByRoles(ids) > 0) {
throw new BadRequestException("所选角色存在用户关联,请解除关联再试!");
}
}
@Override
public List<Role> findInMenuId(List<Long> menuIds) {
return roleRepository.findInMenuId(menuIds);
}
/**
* 清理缓存
* @param id /
*/
public void delCaches(Long id, List<User> users) {
users = CollectionUtil.isEmpty(users) ? userRepository.findByRoleId(id) : users;
if (CollectionUtil.isNotEmpty(users)) {
users.forEach(item -> userCacheManager.cleanUserCache(item.getUsername()));
Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet());
redisUtils.delByKeys(CacheKey.DATA_USER, userIds);View on GitHub (pinned to 55fbf70595)
Solutions
- Edit each affected user and switch them to a replacement role before deleting.
- Remove the rows in sys_users_roles for the target role ids, then retry deletion.
- For bulk migrations, run SQL that reassigns users to a default role, then delete the old roles.
Example fix
-- before: role delete blocked DELETE FROM sys_role WHERE role_id = 3; -- blocked by app -- after: strip user associations first DELETE FROM sys_users_roles WHERE role_id = 3; DELETE FROM sys_role WHERE role_id = 3;
Defensive patterns
Strategy: validation
Validate before calling
// Probe before enabling delete in the UI GET /api/roles/multi?ids=2,3 // non-2xx → keep button disabled, message '所选角色存在用户关联'
Try / catch
catch (BadRequestException e) when (e.getMessage().contains("角色存在用户关联")) { // show affected users and offer reassignment } Prevention
- Always reassign users before deleting roles
- Keep a default fallback role users can be moved to
- Never delete seeded system roles like 超级管理员
When it happens
Trigger: Calling the role delete flow (frontend first probes DELETE /api/roles/multi verification) with role ids still assigned to at least one user in sys_users_roles.
Common situations: Trying to delete the default '普通用户' or '超级管理员' roles that seeded users hold; cleaning up roles after a permission redesign without reassigning users first.
Related errors
- 所选部门存在角色关联,请解除后再试!
- 所选部门存在用户关联,请解除后再试!
- 所选的岗位中存在用户关联,请解除关联再试!
- A new role cannot already have an ID
- 权限不足,你的角色级别:{min},低于操作的角色级别:{level}
AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14).
Data as JSON: /api/errors/d32b6a0f94659070.
Report an issue: GitHub.