Software Development/Trial-and-error

InvalidDataAccessResourceUsageException: could not prepare statement; SQL

찐빵1 2024. 1. 29. 17:45

문제 상황

특정 테이블을 만들고, 테스트코드를 작성 및 실행하던 도중(Repository 테스트)

org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [insert into post_comment (id, created_by, created_dt, deleted, updated_by, updated_dt, version, content, hate_count, like_count, parent_comment_id, pinned, post_id, reply_count, user_id) values (default, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement

 

이라는 에러를 맞이했다.

 

의심 부분

1. 테이블을 생성할 때, 다른 테이블과 의존관계가 있는데 이를 import 해주지 않았나?

-> 해당 부분은 없었다.

 

2. Service 레이어에 문제가 있나?

-> 정신이 나간 생각이다. 단위 테스트고 Repository 테스트를 하는데 다른 레이어의 문제가 생겼을 리가 없다.

 

3. 코드에 문제가 있나?

-> 도저히 봐도 보이지 않았다. 

 

실행해본 전략

1. 저기서 문제가 발생하는 SQL Query를, Datagrip을 열어서 직접 넣어봤다.

-> 잘 들어갔다.

 

간과한 사실

테스트 코드에서 돌아가는 데이터베이스는 H2 Database였고, Datagrip을 통해 확인했던 DB는 AWS Aurora DB였다.

 

진짜 문제상황

에러 메세지를 잘 살펴보니, DB에 해당 테이블 자체가 생성되지 않고 있었다.

 

원인

    @Column(columnDefinition = "TINYINT(1) default 0")
    private Boolean pinned;

 

여기서, columnDefinition이 있는 테이블들은 모두 생성되지 않았다...

이를 제거해주고, nullable = false 처리를 해주니 테이블이 정상 작동하였다.

 

상세 원인 분석

H2 Database는 TINYINT를 지원하지 않았다....

그래서 Aurora DB에서는 정상 작동하고, 테스트 코드는 잘 작동하지 않았던 것.

https://stackoverflow.com/questions/63390995/getting-mappingexception-when-reading-tinyint-value-from-h2-database-as-boolean

 

Getting MappingException when reading TINYINT value from H2 database as boolean/Boolean

I am using R2DBC H2 for integration testing my reactive application (which is based on Spring Webflux , Java). Versions Driver: io.r2dbc:r2dbc-h2:0.8.4.RELEASE (Spring boot 2.3.2.RELEASE) Java: 11...

stackoverflow.com