elunez/eladmin · error · BadRequestException

所选部门存在角色关联,请解除后再试!

Error message

所选部门存在角色关联,请解除后再试!

What it means

Thrown by DeptServiceImpl.verification (line 248) during dept deletion when roleRepository.countByDepts(deptIds) > 0 — at least one role's data scope still references a selected department. This is the second gate after the user check: roles with custom data-scope (数据权限) must not point at departments being removed.

Source

Thrown at eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java:248

        }

        if (CollectionUtil.isEmpty(trees)) {
            trees = depts;
        }
        Map<String,Object> map = new HashMap<>(2);
        map.put("totalElements",deptDtos.size());
        map.put("content",CollectionUtil.isEmpty(trees)? deptDtos :trees);
        return map;
    }

    @Override
    public void verification(Set<DeptDto> deptDtos) {
        Set<Long> deptIds = deptDtos.stream().map(DeptDto::getId).collect(Collectors.toSet());
        if(userRepository.countByDepts(deptIds) > 0){
            throw new BadRequestException("所选部门存在用户关联,请解除后再试!");
        }
        if(roleRepository.countByDepts(deptIds) > 0){
            throw new BadRequestException("所选部门存在角色关联,请解除后再试!");
        }
    }

    private void updateSubCnt(Long deptId){
        if(deptId != null){
            int count = deptRepository.countByPid(deptId);
            deptRepository.updateSubCntById(count, deptId);
        }
    }

    private List<DeptDto> deduplication(List<DeptDto> list) {
        List<DeptDto> deptDtos = new ArrayList<>();
        for (DeptDto deptDto : list) {
            boolean flag = true;
            for (DeptDto dto : list) {
                if (dto.getId().equals(deptDto.getPid())) {
                    flag = false;
                    break;

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Edit each affected role's data scope (remove the dept or switch scope to 全部/本级) in the role management UI, then retry.
  2. Find offenders: SELECT role_id, dept_id FROM sys_roles_depts WHERE dept_id IN (...).
  3. For bulk cleanups, script the sys_roles_depts cleanup before calling DELETE /api/dept.
Defensive patterns

Strategy: validation

Validate before calling

// Server-side wrapper before dept delete
deptDtos.forEach(d -> {
  if (userRepository.countByDepts(Set.of(d.getId())) > 0) throw new BadRequestException("users attached");
  if (roleRepository.countByDepts(Set.of(d.getId())) > 0) throw new BadRequestException("roles attached");
});
deptService.delete(ids);

Try / catch

Catch the 400 and point the operator to role data-scope editing; the message already says which reference class blocks deletion.

Prevention

When it happens

Trigger: A role with dataScope = '自定义' whose sys_roles_depts rows include one of the deleted depts; deleting a subtree that a manager role's custom data scope was scoped to; leftover role-dept mappings from an old org structure.

Common situations: Org restructures where role data scopes were configured per-department years earlier; admins seeing the user check pass but the role check fail on the same batch; test roles with custom scopes pointing at scratch depts.

Related errors


AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14). Data as JSON: /api/errors/84a3a4151baea38c. Report an issue: GitHub.