나를 기록하다
article thumbnail
반응형

김영한님의 자바 ORM 표준 JPA 프로그래밍 기초 강의 중 일대다 [1:N]을 듣는데 강의와는 다르게 나의 H2 DB에는 MEMBER에 TEAM_ID 컬럼이 등장하지 않았다. 외래키가 제대로 추가되지 않을 것으로 생각되었고, drop table을 시도했지만 불가능했다.

drop table이 불가능했던 원인을 살펴보니

<code />
Cannot drop "TEAM" because "FK4U1NPO283VGQFK8LYXCLIHGNL" depends on it; SQL statement: drop table Team if exists [90107-200] 90107/90107 (도움말) org.h2.jdbc.JdbcSQLSyntaxErrorException: Cannot drop "TEAM" because "FK4U1NPO283VGQFK8LYXCLIHGNL" depends on it; SQL statement: drop table Team if exists [90107-200] at org.h2.message.DbException.getJdbcSQLException(DbException.java:576) at org.h2.message.DbException.getJdbcSQLException(DbException.java:429) at org.h2.message.DbException.get(DbException.java:205) at org.h2.command.ddl.DropTable.prepareDrop(DropTable.java:98) at org.h2.command.ddl.DropTable.update(DropTable.java:124) at org.h2.command.CommandContainer.update(CommandContainer.java:198) at org.h2.command.Command.executeUpdate(Command.java:251) at org.h2.server.TcpServerThread.process(TcpServerThread.java:406) at org.h2.server.TcpServerThread.run(TcpServerThread.java:183) at java.lang.Thread.run(Thread.java:748) at org.h2.message.DbException.getJdbcSQLException(DbException.java:576) at org.h2.engine.SessionRemote.done(SessionRemote.java:611) at org.h2.command.CommandRemote.executeUpdate(CommandRemote.java:237) at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:228) at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:201) at org.h2.server.web.WebApp.getResult(WebApp.java:1459) at org.h2.server.web.WebApp.query(WebApp.java:1116) at org.h2.server.web.WebApp$1.next(WebApp.java:1078) at org.h2.server.web.WebApp$1.next(WebApp.java:1065) at org.h2.server.web.WebThread.process(WebThread.java:178) at org.h2.server.web.WebThread.run(WebThread.java:94) at java.lang.Thread.run(Thread.java:748)

이런 이유가 나왔는데 이것은 이 테비을에 의존하는 외래키 제약 조건 "FK4U1NPO283VGQFK8LYXCLIHGNL" 때문에 삭제할 수 없다는 것을 나타낸다.

이 문제를 해결하려면 먼저 외래키 제약 조건을 삭제한 다음 테이블을 삭제해야 한다.

따라서

<code />
ALTER TABLE Member DROP CONSTRAINT FK4U1NPO283VGQFK8LYXCLIHGNL;
<code />
ALTER TABLE Team DROP CONSTRAINT FKPSJMEA2E134AB3X3GSDOYXEPG;

외래키 제약조건을 먼저 삭제해주었고,

<code />
DROP TABLE TEAM IF EXISTS;
<code />
DROP TABLE MEMBER IF EXISTS;

TEAM과 MEMBER 테이블을 삭제해준 뒤, 다시 JpaMain을 실행시켰더니

강의와 똑같이 TEAM_ID가 외래키로 추가된 상태의 H2 DB를 마주할 수 있었다.

반응형
profile

나를 기록하다

@prao

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!