elunez/eladmin · warning · BadRequestException

所选的岗位中存在用户关联,请解除关联再试!

Error message

所选的岗位中存在用户关联,请解除关联再试!

What it means

Thrown by JobServiceImpl.verification when deleting jobs that are still referenced by users. It first counts users whose jobs collection intersects the given ids (userRepository.countByJobs); if any user is still assigned one of the jobs, deletion is blocked. This is referential-integrity protection so users are never left pointing at a deleted job. It surfaces as a 400 Bad Request via the global exception handler.

Source

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

    }

    @Override
    public void download(List<JobDto> jobDtos, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (JobDto jobDTO : jobDtos) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("岗位名称", jobDTO.getName());
            map.put("岗位状态", jobDTO.getEnabled() ? "启用" : "停用");
            map.put("创建日期", jobDTO.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void verification(Set<Long> ids) {
        if(userRepository.countByJobs(ids) > 0){
            throw new BadRequestException("所选的岗位中存在用户关联,请解除关联再试!");
        }
    }

    /**
     * 删除缓存
     * @param id /
     */
    public void delCaches(Long id){
        redisUtils.del(CacheKey.JOB_ID + id);
    }
}

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Open the user management page, filter users by the job in question, and clear/reassign that job on each user before deleting.
  2. Alternatively delete or update the offending rows in the sys_users_jobs join table directly (SQL), then retry the delete.
  3. If bulk cleanup is intended, write a maintenance script/SQL that first sets users_jobs references to NULL or removes them, then deletes the jobs.
  4. In the UI, the verification endpoint /api/jobs/multi can be probed first so the user gets this message before a destructive call.

Example fix

-- before: delete fails because users still reference job 5
DELETE FROM sys_job WHERE job_id = 5; -- app blocks it

-- after: remove user associations first, then delete
DELETE FROM sys_users_jobs WHERE job_id = 5;
DELETE FROM sys_job WHERE job_id = 5;
Defensive patterns

Strategy: validation

Validate before calling

// Before showing Delete, ask the backend verification endpoint
GET /api/jobs/multi?ids=1,2,3
// 200 → safe to delete; 400 with message → block button and show which users hold the job

Try / catch

catch (BadRequestException e) when (e.getMessage().contains("岗位中存在用户关联")) { // surface user reassignment dialog }

Prevention

When it happens

Trigger: Calling DELETE /api/jobs with a set of job ids where at least one id appears in some user's job list (job_user join table has rows). The frontend job list 'delete' action after selecting one or more jobs.

Common situations: Seeding/demo data that assigns jobs to the admin user; trying to clear default jobs before migrating job data; batch-cleaning jobs after an organizational restructure without first reassigning users.

Related errors


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