[HY000] [3780] MySQL 에러 해결하기

이준석·2022년 6월 9일
1

DB

목록 보기
1/1
post-thumbnail

MySQL 3780 에러는 컬럼 혹은 테이블간의 타입이 맞지 않아서 발생한 문제다.
따라서 크게 두가지를 확인해보도록 한다.

  1. 외래키를 맺으려고 하는 두개의 컬럼이 동일한 타입인지 확인한다. 따라서 아래의 명령어를 이용해서 두 테이블에서 외래키에 사용할 컬럼 타입이 동일한지 확인해본다.
# 테이블 컬럼 타입 조회
$ desc <테이블명>;
ex) desc notifications;
+------------+--------------+------+-----+-------------------+-------------------+
| Field      | Type         | Null | Key | Default           | Extra             |
+------------+--------------+------+-----+-------------------+-------------------+
| id         | int          | NO   | PRI | NULL              | auto_increment    |
| title      | varchar(30)  | NO   |     | NULL              |                   |
| type       | int          | NO   |     | NULL              |                   |
| contents   | varchar(100) | NO   |     | NULL              |                   |
| isRead     | tinyint      | NO   |     | 0                 |                   |
| target_url | varchar(255) | YES  |     | NULL              |                   |
| date       | datetime     | NO   |     | NULL              |                   |
| created_at | datetime     | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
| updated_at | datetime     | YES  |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
+------------+--------------+------+-----+-------------------+-------------------+
  1. 컬럼 타입도 동일한데 계속해서 외래키가 incompatible 한 에러가 발생하면 테이블의 character set 이 동일하지 않기 때문이다. 따라서 다음 명령어를 입력해서 동일한 테이블 character set 인지 확인해 줄 수 있도록 한다. (별도로 character set 을 지정하지 않았을 경우 latin1 계열로 설정되어있을 것이다.)
$ show full columns from <테이블명>;
ex) show full columns from notifications;
+------------+--------------+--------------------+------+-----+-------------------+-------------------+---------------------------------+---------+
| Field      | Type         | Collation          | Null | Key | Default           | Extra             | Privileges                      | Comment |
+------------+--------------+--------------------+------+-----+-------------------+-------------------+---------------------------------+---------+
| id         | int          | NULL               | NO   | PRI | NULL              | auto_increment    | select,insert,update,references |         |
| title      | varchar(30)  | utf8mb4_unicode_ci | NO   |     | NULL              |                   | select,insert,update,references |         |
| type       | int          | NULL               | NO   |     | NULL              |                   | select,insert,update,references |         |
| contents   | varchar(100) | utf8mb4_unicode_ci | NO   |     | NULL              |                   | select,insert,update,references |         |
| isRead     | tinyint      | NULL               | NO   |     | 0                 |                   | select,insert,update,references |         |
| target_url | varchar(255) | utf8mb4_unicode_ci | YES  |     | NULL              |                   | select,insert,update,references |         |
| date       | datetime     | NULL               | NO   |     | NULL              |                   | select,insert,update,references |         |
| created_at | datetime     | NULL               | NO   |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED | select,insert,update,references |         |
| updated_at | datetime     | NULL               | YES  |     | CURRENT_TIMESTAMP | DEFAULT_GENERATED | select,insert,update,references |         |
+------------+--------------+--------------------+------+-----+-------------------+-------------------+---------------------------------+---------+

Collationutfmb4_unicode_ci 로 보이는 부분이 테이블의 character set 설정이다. varchar, char 의 타입을 가진 컬럼에만 나온다. 따라서 varchar, char 로 외래키를 엮으려고 하는데 에러가 난다면 Collation 값이 동일한지 확인해본다.

끝.

profile
호주 워홀중

0개의 댓글