Django unable to save unicode string in MySQL (OperationalError – 1366, “Incorrect string value”)

I’ve got a TextField in one of my models. I tried inserting a string of Japanse characters into the database and I got this error:

OperationalError at /admin/pages/page/add/
(1366, "Incorrect string value: '\xE3\x83\x91\xE3\x83\xAF...' for column 'body' at row 1")

I thought that Django, Python, and MySQL supported Unicode and uses it first. What is going on and how can I fix it?

Answer

It is not the Python/Django related issue. Your MySQL table column doesn’t supports the unicode format you are currently using.

Default character set used by MySQL is utf-8. If you want to change character set for any particular column, you may run the query as:

ALTER TABLE db.table MODIFY COLUMN my_column VARCHAR(255)
CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;

where:

  • db: your database
  • table: name of the table
  • my_column: name of column you want to modify