Django 에서 아래와 같은 오류 발생
django.db.utils.OperationalError: (1366, “Incorrect string value: ‘\\xE2\\x80\\x8B9F-…’ for column ‘address’ at row 1”)
원인은 mysql 데이타베이스와 테이블의 문자셋이 utf-8이 아니어서 발생했다.
database의 문자셋을 확인해보면, latin1 이다.
mysql> status; -------------- mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper Connection id: 462073 Current database: information Current user: information@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/run/mysqld/mysqld.sock Uptime: 89 days 8 hours 44 min 3 sec Threads: 1 Questions: 18081364 Slow queries: 0 Opens: 920 Flush tables: 1 Open tables: 846 Queries per second avg: 2.341 --------------
문자셋을 utf-8로 변경한다.
mysql> alter database information character set = utf8mb4 collate = utf8mb4_unicode_ci; Query OK, 1 row affected (0.00 sec) mysql> status; -------------- mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper Connection id: 462073 Current database: information Current user: information@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.25-0ubuntu0.18.04.2 (Ubuntu) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: utf8mb4 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/run/mysqld/mysqld.sock Uptime: 89 days 8 hours 45 min 56 sec Threads: 1 Questions: 18081591 Slow queries: 0 Opens: 920 Flush tables: 1 Open tables: 846 Queries per second avg: 2.341
수정하고 다시 같은 오류 발생하는 경우, 테이블의 문자셋도 확인해보면 latin1이다. 아래 명령어로 테이블 문자셋도 utf-8로 변경한다.
mysql> alter table INFO change orgaddr orgaddr varchar(512) character set utf8mb4 collate utf8mb4_unicode_ci; Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0