[Failed to add the foreign key constraint. Missing index for constraint 'FKe6nwysthx2dnq1ikoe3u9gkxp' in the referenced table 'area']
์ด ERROR๊ฐ ์ฐธ ๊ฑฐ์ฌ๋ ธ๋ค.
ERROR์์๋ ๋ฐ์ดํฐ๊ฐ ์๋ง๊ฒ ๋ค์ด๊ฐ๊ธฐ ๋๋ฌธ์ด๋ค.
๋์ถฉ ์ด๋ค ์๋ฌ์ธ์ง๋ ํ์ ํ๋ค.
area_code๊ฐ unique ์กฐ๊ฑด์ด ๊ฑธ๋ ค์์ง ์๊ธฐ ๋๋ฌธ์ FK ์ ์ฝ์กฐ๊ฑด์ ์์ฑํ ์ ์๋ค๋ ๊ฒ์ด๋ค.
์ด๋ค ์ํฉ์ธ์ง...
Weather ์ํฐํฐ์ Area ์ํฐํฐ๊ฐ ์ผ๋์ผ ๋งคํ๋์ด์๊ณ ,
referencedColumnName ์ต์ ์ ์ค์ AREA_CODE๋ก Area ํ๋๋ฅผ ์ฐธ์กฐํ๋ค.
@Entity
@Table(name = "WEATHER")
public class Weather {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "WEATHER_ID")
private Long weatherId;
...
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "AREA_CODE", referencedColumnName = "AREA_CODE")
private Area area;
}
@Column(unique=true)
Area ์ํฐํฐ์ areaCode์ unique=true๋ก ์ ๋ํฌ ์ ์ฝ ์กฐ๊ฑด์ ๊ฑธ์ด์ค๋ค.
@Entity
@Table(name = "area")
public class Area {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "AREA_ID")
private Long areaId;
@Column(name = "AREA_CODE", unique=true)
private String areaCode;
@Column(name = "AREA_NAME")
private String areaName;
}

๊ทธ๋ฌ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
@UniqueConstraint
@Entity
@Table(name = "area", uniqueConstraints={@UniqueConstraint(columnNames={"AREA_CODE"})})
public class Area {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "AREA_ID")
private Long areaId;
@Column(name = "AREA_CODE")
private String areaCode;
@Column(name = "AREA_NAME")
private String areaName;
}
๋๋ Table ์์ฒด์ ์ ์ฝ ์กฐ๊ฑด์ ์ค์ ํ ์ ์๋ค.
@UniqueConstraint ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ฉด ๋๋๋ฐ ์ด ์ด๋ ธํ ์ด์ ์ ์ฃผ๋ก ๋ณตํฉ ์ ๋ํฌ ์ ์ฝ ์กฐ๊ฑด์ ์ ์ํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
์๋์ ๊ฐ์ด ๋ค์์ ์ปฌ๋ผ๋ค์ ์ ์ฉ์ํฌ ๋ ์ฃผ๋ก ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "personNumber", "isActive" }) })
๐ ์กฐ๊ฑด์ ๊ฑธ์ด์ค ํ create-drop์ ํตํด ํ ์ด๋ธ์ dropํ ํ ๋ค์ ์์ฑํ๋ ๊ฒ ์์ง ๋ง์ !
์ฐธ๊ณ ์๋ฃ
https://www.baeldung.com/jpa-unique-constraints
'Database > JPA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JPA] Open-in-view์ @Transactional (0) | 2024.04.08 |
---|---|
[JPA] JPA Entity๊ฐ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๊ฐ์ ธ์ผํ๋ ์ด์ - Reflection (1) | 2024.03.16 |
[Failed to add the foreign key constraint. Missing index for constraint 'FKe6nwysthx2dnq1ikoe3u9gkxp' in the referenced table 'area']
์ด ERROR๊ฐ ์ฐธ ๊ฑฐ์ฌ๋ ธ๋ค.
ERROR์์๋ ๋ฐ์ดํฐ๊ฐ ์๋ง๊ฒ ๋ค์ด๊ฐ๊ธฐ ๋๋ฌธ์ด๋ค.
๋์ถฉ ์ด๋ค ์๋ฌ์ธ์ง๋ ํ์ ํ๋ค.
area_code๊ฐ unique ์กฐ๊ฑด์ด ๊ฑธ๋ ค์์ง ์๊ธฐ ๋๋ฌธ์ FK ์ ์ฝ์กฐ๊ฑด์ ์์ฑํ ์ ์๋ค๋ ๊ฒ์ด๋ค.
์ด๋ค ์ํฉ์ธ์ง...
Weather ์ํฐํฐ์ Area ์ํฐํฐ๊ฐ ์ผ๋์ผ ๋งคํ๋์ด์๊ณ ,
referencedColumnName ์ต์ ์ ์ค์ AREA_CODE๋ก Area ํ๋๋ฅผ ์ฐธ์กฐํ๋ค.
@Entity
@Table(name = "WEATHER")
public class Weather {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "WEATHER_ID")
private Long weatherId;
...
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "AREA_CODE", referencedColumnName = "AREA_CODE")
private Area area;
}
@Column(unique=true)
Area ์ํฐํฐ์ areaCode์ unique=true๋ก ์ ๋ํฌ ์ ์ฝ ์กฐ๊ฑด์ ๊ฑธ์ด์ค๋ค.
@Entity
@Table(name = "area")
public class Area {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "AREA_ID")
private Long areaId;
@Column(name = "AREA_CODE", unique=true)
private String areaCode;
@Column(name = "AREA_NAME")
private String areaName;
}

๊ทธ๋ฌ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ ๋์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
@UniqueConstraint
@Entity
@Table(name = "area", uniqueConstraints={@UniqueConstraint(columnNames={"AREA_CODE"})})
public class Area {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "AREA_ID")
private Long areaId;
@Column(name = "AREA_CODE")
private String areaCode;
@Column(name = "AREA_NAME")
private String areaName;
}
๋๋ Table ์์ฒด์ ์ ์ฝ ์กฐ๊ฑด์ ์ค์ ํ ์ ์๋ค.
@UniqueConstraint ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ๋ฉด ๋๋๋ฐ ์ด ์ด๋ ธํ ์ด์ ์ ์ฃผ๋ก ๋ณตํฉ ์ ๋ํฌ ์ ์ฝ ์กฐ๊ฑด์ ์ ์ํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
์๋์ ๊ฐ์ด ๋ค์์ ์ปฌ๋ผ๋ค์ ์ ์ฉ์ํฌ ๋ ์ฃผ๋ก ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
@Table(uniqueConstraints = { @UniqueConstraint(columnNames = { "personNumber", "isActive" }) })
๐ ์กฐ๊ฑด์ ๊ฑธ์ด์ค ํ create-drop์ ํตํด ํ ์ด๋ธ์ dropํ ํ ๋ค์ ์์ฑํ๋ ๊ฒ ์์ง ๋ง์ !
์ฐธ๊ณ ์๋ฃ
https://www.baeldung.com/jpa-unique-constraints
'Database > JPA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JPA] Open-in-view์ @Transactional (0) | 2024.04.08 |
---|---|
[JPA] JPA Entity๊ฐ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๊ฐ์ ธ์ผํ๋ ์ด์ - Reflection (1) | 2024.03.16 |