{"record":{"id":"d32b6a0f94659070","repo":"elunez/eladmin","slug":"error-d32b6a","errorCode":null,"errorMessage":"所选角色存在用户关联，请解除关联再试！","messagePattern":"所选角色存在用户关联，请解除关联再试！","errorType":"exception","errorClass":"BadRequestException","httpStatus":400,"severity":"warning","filePath":"eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java","lineNumber":213,"sourceCode":"\n    @Override\n    public void download(List<RoleDto> roles, HttpServletResponse response) throws IOException {\n        List<Map<String, Object>> list = new ArrayList<>();\n        for (RoleDto role : roles) {\n            Map<String, Object> map = new LinkedHashMap<>();\n            map.put(\"角色名称\", role.getName());\n            map.put(\"角色级别\", role.getLevel());\n            map.put(\"描述\", role.getDescription());\n            map.put(\"创建日期\", role.getCreateTime());\n            list.add(map);\n        }\n        FileUtil.downloadExcel(list, response);\n    }\n\n    @Override\n    public void verification(Set<Long> ids) {\n        if (userRepository.countByRoles(ids) > 0) {\n            throw new BadRequestException(\"所选角色存在用户关联，请解除关联再试！\");\n        }\n    }\n\n    @Override\n    public List<Role> findInMenuId(List<Long> menuIds) {\n        return roleRepository.findInMenuId(menuIds);\n    }\n\n    /**\n     * 清理缓存\n     * @param id /\n     */\n    public void delCaches(Long id, List<User> users) {\n        users = CollectionUtil.isEmpty(users) ? userRepository.findByRoleId(id) : users;\n        if (CollectionUtil.isNotEmpty(users)) {\n            users.forEach(item -> userCacheManager.cleanUserCache(item.getUsername()));\n            Set<Long> userIds = users.stream().map(User::getId).collect(Collectors.toSet());\n            redisUtils.delByKeys(CacheKey.DATA_USER, userIds);","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/elunez/eladmin/blob/55fbf705956949697dbd68bf9003776609d3d029/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java#L195-L231","documentation":"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.","triggerScenarios":"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.","commonSituations":"Trying to delete the default '普通用户' or '超级管理员' roles that seeded users hold; cleaning up roles after a permission redesign without reassigning users first.","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."],"exampleFix":"-- before: role delete blocked\nDELETE FROM sys_role WHERE role_id = 3; -- blocked by app\n\n-- after: strip user associations first\nDELETE FROM sys_users_roles WHERE role_id = 3;\nDELETE FROM sys_role WHERE role_id = 3;","handlingStrategy":"validation","validationCode":"// Probe before enabling delete in the UI\nGET /api/roles/multi?ids=2,3\n// non-2xx → keep button disabled, message '所选角色存在用户关联'","typeGuard":null,"tryCatchPattern":"catch (BadRequestException e) when (e.getMessage().contains(\"角色存在用户关联\")) { // show affected users and offer reassignment }","preventionTips":["Always reassign users before deleting roles","Keep a default fallback role users can be moved to","Never delete seeded system roles like 超级管理员"],"tags":["eladmin","role","referential-integrity","delete"],"backgroundTag":null,"analyzedSha":"55fbf705956949697dbd68bf9003776609d3d029","analyzedAt":"2026-08-14T11:56:12.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}