외래키 제약 조건 위반 에러 회고


에러 내용

직원 삭제 API 실행 시 본래 기능은 해당 직원 db에서 삭제 하지만 changelog db 에는 이력 존재

WARN 71661 --- [nio-8080-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 23503

ERROR 71661 --- [nio-8080-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper   : 
ERROR: update or delete on table "employees" violates foreign key constraint "change_logs_employee_id_fkey" on table "change_logs"
 
Detail: Key (id)=(23) is still referenced from table "change_logs".

ERROR 71661 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.RuntimeException: 직원 삭제 중 오류가 발생했습니다.] with root cause


원인 분석

직원이 삭제 되면서 id를 참조하고있는 employeenumber도 같이 삭제 되면서 해당 에러 발생


해결 방법

해당 기능 구현 방향을 변경

image.png