1. batchDelete使用中的问题
正常使用
15:12:14.900 [http-nio-8080-exec-98] DEBUG c.r.s.m.S.deleteById - [debug,135] - ==> Preparing: DELETE FROM subject WHERE subject_id=? 15:12:14.901 [http-nio-8080-exec-98] DEBUG c.r.s.m.S.deleteById - [debug,135] - ==> Parameters: 35(Long) 15:12:14.901 [http-nio-8080-exec-98] DEBUG c.r.s.m.S.deleteById - [debug,135] - ==> Parameters: 36(Long) 15:12:14.901 [http-nio-8080-exec-98] DEBUG c.r.s.m.S.deleteById - [debug,135] - ==> Parameters: 42(Long) 15:12:14.901 [http-nio-8080-exec-98] DEBUG c.r.s.m.S.deleteById - [debug,135] - ==> Parameters: 43(Long) 15:12:14.901 [http-nio-8080-exec-98] DEBUG c.r.s.m.S.deleteById - [debug,135] - ==> Parameters: 44(Long) 15:12:14.901 [http-nio-8080-exec-98] DEBUG c.r.s.m.S.deleteById - [debug,135] - ==> Parameters: 45(Long) 15:12:14.901 [http-nio-8080-exec-98] DEBUG c.r.s.m.S.deleteById - [debug,135] - ==> Parameters: 46(Long) 15:12:14.902 [http-nio-8080-exec-98] DEBUG c.r.s.m.S.deleteById - [debug,135] - ==> Parameters: 47(Long) 15:12:14.902 [http-nio-8080-exec-98] DEBUG c.r.s.m.S.deleteById - [debug,135] - ==> Parameters: 48(Long) 15:12:14.902 [http-nio-8080-exec-98] DEBUG c.r.s.m.S.deleteById - [debug,135] - ==> Parameters: 49(Long)
1.2 发生异常
后端报错
org.springframework.dao.DataIntegrityViolationException: com.ruoyi.system.mapper.SubjectMapper.deleteById (batch index #1) failed. Cause: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (ruoyi
.major_subject
, CONSTRAINT major_subject_ibfk_2
FOREIGN KEY (subject_id
) REFERENCES subject
(subject_id
) ON DELETE RESTRICT ON UPDATE RESTRICT) ; Cannot delete or update a parent row: a foreign key constraint fails (ruoyi
.major_subject
, CONSTRAINT major_subject_ibfk_2
FOREIGN KEY (subject_id
) REFERENCES subject
(subject_id
) ON DELETE RESTRICT ON UPDATE RESTRICT); nested exception is java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (ruoyi
.major_subject
, CONSTRAINT major_subject_ibfk_2
FOREIGN KEY (subject_id
) REFERENCES subject
(subject_id
) ON DELETE RESTRICT ON UPDATE RESTRICT)
前端报错
com.ruoyi.system.mapper.SubjectMapper.deleteById (batch index #1) failed. Cause: java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (ruoyi
.major_subject
, CONSTRAINT major_subject_ibfk_2
FOREIGN KEY (subject_id
) REFERENCES subject
(subject_id
) ON DELETE RESTRICT ON UPDATE RESTRICT) ; Cannot delete or update a parent row: a foreign key constraint fails (ruoyi
.major_subject
, CONSTRAINT major_subject_ibfk_2
FOREIGN KEY (subject_id
) REFERENCES subject
(subject_id
) ON DELETE RESTRICT ON UPDATE RESTRICT); nested exception is java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (ruoyi
.major_subject
, CONSTRAINT major_subject_ibfk_2
FOREIGN KEY (subject_id
) REFERENCES subject
(subject_id
) ON DELETE RESTRICT ON UPDATE RESTRICT)
2. 错误原因
在删除subject表中的数据时,major_subject表中的数据没有进行更新,导致数据不同步而发生的报错
3. 解决方案
先将子表major_subject表中subject_id对应的数据删除后再从主表subject表中删除数据
public boolean batchDeleteSubjectInformation(@PathVariable Long[] subjectIds) { List<Long> sIds = Arrays.asList(subjectIds); // 删除子表中的数据 for (int temp = 0; temp < subjectIds.length; temp++) { subjectService.removeMajorById(subjectIds[temp]); } return subjectService.removeBatchByIds(sIds); }