foreign key가 있는 테이블을 만들려고 하자, 에러가 났다.
create table personApplyStudent ( Id BIGINT auto_increment PRIMARY KEY, person_id BIGINT, student_id BIGINT, FOREIGN KEY (person_id) REFERENCES senior_junior (senior_junior_id) );
ERROR 1215 (HY000): Cannot add foreign key constraint
다음 명령어로 더 상세한 원인 로그를 찾았다.
SHOW ENGINE INNODB STATUS;
2024-05-01 16:15:49 0x70000165b000 Error in foreign key constraint of table db/personapplystudent:
FOREIGN KEY (person_id) REFERENCES senior_junior (senior_junior_id) ):
Cannot resolve table name close to:
(senior_junior_id) )
테이블 이름이 잘못된 것은 아니었다.
에러 로그로 더 알 수 있는 내용은 없었다.
알고 보니 엔진의 문제였다.
아래 명령어를 실행한다.
show create table senior_junior;
senior_junior | CREATE TABLE `senior_junior` (
`senior_junior_id` bigint(20) NOT NULL,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`senior_junior_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
엔진이 MyISAM인 것을 확인한다.
MyISAM은 foreign key를 적용할 수 없다고 한다.
https://mariadb.com/kb/en/myisam-overview/