{"record":{"id":"f38284f74a42cabc","repo":"elunez/eladmin","slug":"a-new-dict-cannot-already-have-an-id","errorCode":null,"errorMessage":"A new dict cannot already have an ID","messagePattern":"A new dict cannot already have an ID","errorType":"exception","errorClass":"BadRequestException","httpStatus":400,"severity":"warning","filePath":"eladmin-system/src/main/java/me/zhengjie/modules/system/rest/DictController.java","lineNumber":80,"sourceCode":"    @PreAuthorize(\"@el.check('dict:list')\")\n    public ResponseEntity<List<DictDto>> queryAllDict(){\n        return new ResponseEntity<>(dictService.queryAll(new DictQueryCriteria()),HttpStatus.OK);\n    }\n\n    @ApiOperation(\"查询字典\")\n    @GetMapping\n    @PreAuthorize(\"@el.check('dict:list')\")\n    public ResponseEntity<PageResult<DictDto>> queryDict(DictQueryCriteria resources, Pageable pageable){\n        return new ResponseEntity<>(dictService.queryAll(resources,pageable),HttpStatus.OK);\n    }\n\n    @Log(\"新增字典\")\n    @ApiOperation(\"新增字典\")\n    @PostMapping\n    @PreAuthorize(\"@el.check('dict:add')\")\n    public ResponseEntity<Object> createDict(@Validated @RequestBody Dict resources){\n        if (resources.getId() != null) {\n            throw new BadRequestException(\"A new \"+ ENTITY_NAME +\" cannot already have an ID\");\n        }\n        dictService.create(resources);\n        return new ResponseEntity<>(HttpStatus.CREATED);\n    }\n\n    @Log(\"修改字典\")\n    @ApiOperation(\"修改字典\")\n    @PutMapping\n    @PreAuthorize(\"@el.check('dict:edit')\")\n    public ResponseEntity<Object> updateDict(@Validated(Dict.Update.class) @RequestBody Dict resources){\n        dictService.update(resources);\n        return new ResponseEntity<>(HttpStatus.NO_CONTENT);\n    }\n\n    @Log(\"删除字典\")\n    @ApiOperation(\"删除字典\")\n    @DeleteMapping\n    @PreAuthorize(\"@el.check('dict:del')\")","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/elunez/eladmin/blob/55fbf705956949697dbd68bf9003776609d3d029/eladmin-system/src/main/java/me/zhengjie/modules/system/rest/DictController.java#L62-L98","documentation":"Thrown by DictController.createDict (line 80) when POST /api/dict includes a non-null id in the JSON body. Same eladmin create-contract guard as other controllers: dictionary entries get their identity from the database, and an inbound id on create is rejected with 400 before dictService.create runs.","triggerScenarios":"Reusing the dict edit form (populated with id) for creation; POSTing a DictDto fetched from GET /api/dict back to the create endpoint; bulk-loading dict rows from another environment without removing ids.","commonSituations":"Front-end dialog state not cleared between edit and create; sync scripts copying dict data between dev/prod instances; Swagger testing where the example schema includes id and the tester leaves it filled.","solutions":["Delete the id key from the request payload before calling POST /api/dict.","Reset the form bound object ({ id: null, name: '', remark: '' }) when opening the create dialog.","For cross-environment dict sync, use PUT with existing ids or write a dedicated import endpoint instead of POST-with-id."],"exampleFix":"// before\naxios.post('/api/dict', row) // row came from the list with id set\n// after\nconst { id, ...payload } = row;\naxios.post('/api/dict', payload)","handlingStrategy":"validation","validationCode":"const payload = { name: form.name, remark: form.remark }; // whitelist fields for create\naxios.post('/api/dict', payload);","typeGuard":"const isCreatePayload = (p) => !('id' in p) || p.id == null;","tryCatchPattern":null,"preventionTips":["Whitelist create payload fields instead of spreading the whole entity.","Reset dict form between dialog modes.","In sync scripts, map old rows to { name, remark } before POST."],"tags":["rest-api","validation","eladmin","dict","crud"],"backgroundTag":null,"analyzedSha":"55fbf705956949697dbd68bf9003776609d3d029","analyzedAt":"2026-08-14T11:56:12.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}