{"record":{"id":"6c7328d9cffb5c83","repo":"elunez/eladmin","slug":"a-new-dept-cannot-already-have-an-id","errorCode":null,"errorMessage":"A new dept cannot already have an ID","messagePattern":"A new dept cannot already have an ID","errorType":"exception","errorClass":"BadRequestException","httpStatus":400,"severity":"warning","filePath":"eladmin-system/src/main/java/me/zhengjie/modules/system/rest/DeptController.java","lineNumber":96,"sourceCode":"                    if(dept.getId().equals(deptDto.getPid())) {\n                        dept.setSubCount(dept.getSubCount() - 1);\n                    }\n                }\n                // 编辑部门时不显示自己以及自己下级的数据，避免出现PID数据环形问题\n                depts = depts.stream().filter(i -> !ids.contains(i.getId())).collect(Collectors.toList());\n            }\n            deptSet.addAll(depts);\n        }\n        return new ResponseEntity<>(deptService.buildTree(new ArrayList<>(deptSet)),HttpStatus.OK);\n    }\n\n    @Log(\"新增部门\")\n    @ApiOperation(\"新增部门\")\n    @PostMapping\n    @PreAuthorize(\"@el.check('dept:add')\")\n    public ResponseEntity<Object> createDept(@Validated @RequestBody Dept resources){\n        if (resources.getId() != null) {\n            throw new BadRequestException(\"A new \"+ ENTITY_NAME +\" cannot already have an ID\");\n        }\n        deptService.create(resources);\n        return new ResponseEntity<>(HttpStatus.CREATED);\n    }\n\n    @Log(\"修改部门\")\n    @ApiOperation(\"修改部门\")\n    @PutMapping\n    @PreAuthorize(\"@el.check('dept:edit')\")\n    public ResponseEntity<Object> updateDept(@Validated(Dept.Update.class) @RequestBody Dept resources){\n        deptService.update(resources);\n        return new ResponseEntity<>(HttpStatus.NO_CONTENT);\n    }\n\n    @Log(\"删除部门\")\n    @ApiOperation(\"删除部门\")\n    @DeleteMapping\n    @PreAuthorize(\"@el.check('dept:del')\")","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/elunez/eladmin/blob/55fbf705956949697dbd68bf9003776609d3d029/eladmin-system/src/main/java/me/zhengjie/modules/system/rest/DeptController.java#L78-L114","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\naxios.post('/api/dept', this.form) // this.form.id left over from edit\n// after\nconst { id, ...payload } = this.form;\naxios.post('/api/dept', payload)","handlingStrategy":"validation","validationCode":"function toCreatePayload(dept) {\n  const { id, ...payload } = dept;\n  return payload; // POST /api/dept\n}\n// or guard inline before sending\nif (dept.id != null) delete dept.id;","typeGuard":"const isCreateSafe = (dept) => dept.id === undefined || dept.id === null;","tryCatchPattern":"If a generic poster is unavoidable, catch the 400 and strip id then retry once — but prefer validating before send.","preventionTips":["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."],"tags":["rest-api","validation","eladmin","dept","crud"],"backgroundTag":null,"analyzedSha":"55fbf705956949697dbd68bf9003776609d3d029","analyzedAt":"2026-08-14T11:56:12.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}