elunez/eladmin · warning · BadRequestException
A new dept cannot already have an ID
Error message
A new dept cannot already have an ID
What it means
Thrown by DeptController.createDept (line 96) when a POST /api/dept request body carries a non-null id. eladmin's create convention is strict: the id field is server-generated, so an id in the payload is treated as a client bug or a copy-paste from an edit form. Fails fast before deptService.create so no partial write happens.
Source
Thrown at eladmin-system/src/main/java/me/zhengjie/modules/system/rest/DeptController.java:96
if(dept.getId().equals(deptDto.getPid())) {
dept.setSubCount(dept.getSubCount() - 1);
}
}
// 编辑部门时不显示自己以及自己下级的数据,避免出现PID数据环形问题
depts = depts.stream().filter(i -> !ids.contains(i.getId())).collect(Collectors.toList());
}
deptSet.addAll(depts);
}
return new ResponseEntity<>(deptService.buildTree(new ArrayList<>(deptSet)),HttpStatus.OK);
}
@Log("新增部门")
@ApiOperation("新增部门")
@PostMapping
@PreAuthorize("@el.check('dept:add')")
public ResponseEntity<Object> createDept(@Validated @RequestBody Dept resources){
if (resources.getId() != null) {
throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
}
deptService.create(resources);
return new ResponseEntity<>(HttpStatus.CREATED);
}
@Log("修改部门")
@ApiOperation("修改部门")
@PutMapping
@PreAuthorize("@el.check('dept:edit')")
public ResponseEntity<Object> updateDept(@Validated(Dept.Update.class) @RequestBody Dept resources){
deptService.update(resources);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@Log("删除部门")
@ApiOperation("删除部门")
@DeleteMapping
@PreAuthorize("@el.check('dept:del')")View on GitHub (pinned to 55fbf70595)
Solutions
- Strip the id before POSTing: const { id, ...payload } = dept; axios.post('/api/dept', payload).
- Reset the dept form model (id = null) whenever the create dialog opens.
- If you really need to insert with a fixed id, add a dedicated import endpoint rather than bypassing this guard.
Example fix
// before
axios.post('/api/dept', this.form) // this.form.id left over from edit
// after
const { id, ...payload } = this.form;
axios.post('/api/dept', payload) Defensive patterns
Strategy: validation
Validate before calling
function toCreatePayload(dept) {
const { id, ...payload } = dept;
return payload; // POST /api/dept
}
// or guard inline before sending
if (dept.id != null) delete dept.id; Type guard
const isCreateSafe = (dept) => dept.id === undefined || dept.id === null;
Try / catch
If a generic poster is unavoidable, catch the 400 and strip id then retry once — but prefer validating before send.
Prevention
- Use a dedicated empty form model for create dialogs, never the edit row.
- Reset dialog state (id = null) in the dialog's open hook.
- Keep create and update payloads as separate types in the client.
When it happens
Trigger: Front-end reuses the edit dialog's populated Dept object for create; importing departments via API where each source record includes its external id; a client that serializes the full entity after a GET and POSTs it back unchanged.
Common situations: Vue form not reset between 'edit' and 'new' modes of the dept dialog; data-migration scripts POSTing rows read from another eladmin instance; API testers copying a PUT body into a POST request.
Related errors
- A new dict cannot already have an ID
- A new dictDetail cannot already have an ID
- A new job cannot already have an ID
- A new menu cannot already have an ID
- A new role cannot already have an ID
AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14).
Data as JSON: /api/errors/6c7328d9cffb5c83.
Report an issue: GitHub.