I put this in settings.py
:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': 'localhost',
'NAME': 'db',
'USER': 'root',
'PASSWORD': '',
'OPTIONS': {
'charset': 'utf8mb4',
'init_command': 'set collation_connection=utf8mb4_unicode_ci',
},
},
}
Then I used the shell to check that it worked:
$ ./manage.py shell
>>> from django.db import connection
>>> cursor = connection.cursor()
>>> cursor.execute("show variables like 'collation_connection'")
>>> print cursor.fetchall()
((u'collation_connection', u'utf8mb4_general_ci'),)
Unfortunately, what I've learned from inspecting my query log is that MySQLdb
does this when it connects:
set collation_connection='utf8mb4_unicode_ci'
SET NAMES utf8mb4
That's right. It executes set names
after my init command. This sets the collation back to the default.
Omitting the 'charset'
option doesn't help. If I do, it will call set names utf8
instead, which is even worse. I tried making the set names
command part of my 'init_command'
in case it wouldn't clobber my collation if there was nothing to change, but no, it still clobbers it.
I can't fork the Python library MySQLdb
because I'm running my app on Google App Engine and MySQLdb
is part of App Engine.