This has been documented elsewhere previously but for my own recollection, here it is again.
If you create a typical UTF-8 database (CHARACTER SET utf8 COLLATE utf8_unicode_ci), you’ll run into the following error if you attempt to save 4-byte characters to MySQL (i.e. emoticons). This is due to MySQL’s 3 byte limit on utf-8 characters.
To remedy this issue, you’ll need to make a couple of configuration changes:
1) Switch your MySQL database to the utf8mb4 character set (you’ll need MySQL 5.5 or later).
2) Update your Django database settings to use the utf8mb4 encoding:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATABASES = { | |
'default': { | |
'ENGINE':'django.db.backends.mysql', | |
… | |
'OPTIONS': {'charset': 'utf8mb4'}, | |
} | |
} |
One thing to watch out for, if you have a CharField with a max_length of 255 characters and it has an index on it (i.e. unique), you’ll need to reduce the max_length to 191 as utf8mb4 takes up 33% more space. More info can be found in this Django ticket.