Skip to content

Instantly share code, notes, and snippets.

@minjejeon
Forked from philippeowagner/gist:6530635
Last active August 24, 2020 08:29
Show Gist options
  • Select an option

  • Save minjejeon/0e5562468f83274679e8bfa56fa1937e to your computer and use it in GitHub Desktop.

Select an option

Save minjejeon/0e5562468f83274679e8bfa56fa1937e to your computer and use it in GitHub Desktop.
Having troubles with 'latin1_swedish_ci' on your MySQL Databases? Specially `OperationalError (1267, "Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='")`? No problem, fix it using Django's shell.This solution is based on this http://stackoverflow.com/questions/1073295/django-character-set-w…
from django.db import connection
from django.conf import settings
cursor = connection.cursor()
cursor.execute('SHOW TABLES')
results=[]
for row in cursor.fetchall():
results.append(row)
for row in results:
cursor.execute('ALTER TABLE %s CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;' % (row[0]))
cursor.execute('ALTER DATABASE %s CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;' % settings.DATABASES['default']['NAME'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment