Jpub / SpringInAction5

<스프링 인 액션>(제5판) 리포지터리
99 stars 64 forks source link

스프링부트 3.0 버전 이상에서 schema.sql 오류 발생 #6

Open rooni97 opened 1 year ago

rooni97 commented 1 year ago

3.0 버전 업데이트 되면서 의존성 관련해서 뭔가 많이 바뀌었는데 스프링부트 3.1에서는 h2 2.1.214 버전을 써야하는 걸로 알고 있습니다.

H2 버전 관련 부분은 이 분의 블로그를 참고했습니다. https://velog.io/@gmtmoney2357/스프링-부트-3.0에서-h2-console-404-에러

이 부분에서 문제가 발생 👇

alter table Taco_Ingredients
    add foreign key (ingredient) references Ingredient(id);

처음 스프링 시작할 때 jdbcsqlsyntaxerrorexception: constraint "primary key | unique (id)" not found; 에러가 발생했는데요.

외래키로 거는 컬럼이 unique하지 않으면 외래키 제약 조건 추가가 안되는 것 같습니다.

이전 코드

create table if not exists Ingredient (
id varchar(4) not null,
name varchar(25) not null,
type varchar(10) not null
);

수정 코드

create table if not exists Ingredient (
id varchar(4) PRIMARY KEY not null,
name varchar(25) not null,
type varchar(10) not null
);

PRIMARY KEY를 추가해주니 잘 되는 거 같습니다.