Skip to content

Instantly share code, notes, and snippets.

@flyte
Created November 26, 2020 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flyte/39517caad8358fc90b73e160e228b916 to your computer and use it in GitHub Desktop.
Save flyte/39517caad8358fc90b73e160e228b916 to your computer and use it in GitHub Desktop.
Change MySQL/MariaDB character set and collation to something sane
from django.db import connection
DB_NAME = "your-db-name"
cursor = connection.cursor()
tables = connection.introspection.table_names()
cursor.execute(f"ALTER DATABASE `{DB_NAME}` CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci")
for table in tables:
cursor.execute(f"ALTER TABLE {table} CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment