Database/SQL
[Error] MySQL 에러 Cannot delete or update a parent row: a foreign key constraint fails
헹창
2019. 12. 11. 11:33
반응형
[Error] MySQL 에러 Cannot delete or update a parent row: a foreign key constraint fails
에러 로그
### Cause: java.sql.SQLIntegrityConstraintViolationException: (conn=4502) Cannot delete or update a parent row: a foreign key constraint fails (`taat_vol2`.`file`, CONSTRAINT `file_ibfk_1` FOREIGN KEY (`filegid`) REFERENCES `board` (`id`))
현재 구현 로직은 특정 게시물을 지우면 해당 id값의 board 테이블 데이터와 file 테이블에 조인된 데이터를 삭제한다.
// 컨트롤러 메소드에서 board 삭제 후 file 삭제
service.delBoardData(param);
service.delFileList(param);
테이블
board table
file table
여기서 filegid = board.id 이다
board 테이블의 데이터를 지우고 file 데이터를 지우려니 위와같은 참조키 에러가 발생한다.
자식 객체 삭제 후 부모 객체 삭제
자식 객체인 file 테이블의 데이터를 먼저 삭제하고 그 후에 부모 모델 객체인 board 테이블의 데이터를 삭제하면 에러 해결 !
728x90
반응형