<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Max Fisco on Medium]]></title>
        <description><![CDATA[Stories by Max Fisco on Medium]]></description>
        <link>https://medium.com/@maxfisco?source=rss-65297422fc9c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uDvy8mg-DYEQg9nsAY7iXw.jpeg</url>
            <title>Stories by Max Fisco on Medium</title>
            <link>https://medium.com/@maxfisco?source=rss-65297422fc9c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 04:24:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maxfisco/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Django + MySQL: Migrating from utf8 to utf8mb4]]></title>
            <link>https://blog.devgenius.io/nc-django-mysql-migrating-from-utf8-to-utf8mb4-9d8b2753e632?source=rss-65297422fc9c------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d8b2753e632</guid>
            <category><![CDATA[utf8mb4]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Max Fisco]]></dc:creator>
            <pubDate>Mon, 31 Oct 2022 19:17:07 GMT</pubDate>
            <atom:updated>2022-12-23T18:22:08.576Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NV8nWHIA7zWGS4-IXlBMHA.png" /></figure><h3>From utf8 to utf8mb4 in MySQL 5.7 with Django</h3><p>Didn’t know that MySQL 5.7 defaults to a 3-byte limit on UTF-8 characters? Me neither. But it does, and it’s a problem if you’re using MySQL. This post will show you how to automate migrating your database from utf8 to utf8mb4, in a way that doesn’t break your Django app.</p><h3><strong>Why is this a problem?</strong></h3><p>I won’t go into the nitty-gritty details but the short version is:</p><ol><li>MySQL’s utf8 encoding isn’t UTF-8 so when a user tries to save a 4-byte character, like an emoji 🙃, you’ll start getting errors like this:</li></ol><pre>django.db.utils.OperationalError: (1366, “Incorrect string value: ‘\\xF0\\x9F\\x99\\x83’ for column ‘name’ at row 1”)</pre><p>2. MySQL 5.7 reaches end-of-life in October of 2023 and its default encoding is utf8, whereas MySQL 8, the default encoding is now utf8mb4. So eventually you’ll want to migrate to utf8mb4 anyway — or have a bunch of mismatched character sets across tables.</p><p>If you’re interested in the <em>why</em>, these two articles do a great job:</p><ul><li><a href="https://saveriomiroddi.github.io/An-in-depth-dbas-guide-to-migrating-a-mysql-database-from-the-utf8-to-the-utf8mb4-charset/">An in depth DBA’s guide to migrating a MySQL database from the `utf8` to the `utf8mb4` charset</a></li><li><a href="https://mathiasbynens.be/notes/mysql-utf8mb4">How to support full Unicode in MySQL databases</a></li></ul><h3>How to fix it?</h3><p><strong>Step 1: Safety first<br></strong>Create a backup using the awesome <a href="https://github.com/jazzband/django-dbbackup">django-dbbackup</a> app or with <strong>mysqldump</strong> like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZuycOBJScD9YaZxL_nL7ZQ.png" /><figcaption>mysqldump command to backup the database</figcaption></figure><p><strong>Step 2: Update your Django settings<br></strong>If you haven’t already done so, in your <em>settings.py</em> make sure you’re using utf8mb4 as the default encoding. This tells the MySQL server that your app will be sending it utf8mb4 encoded data (instead of the utf8 default).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NQjO0g7JLdvVB0u4VcOTSg.png" /><figcaption>Database configuration in settings.py</figcaption></figure><p><strong>Step 3: Update your database, tables, and columns to use utf8mb4<br></strong>Historically, this was where things became a bit tedious. Before MySQL 5.7, you would need to create custom statements for all text-based columns and indexes based on their data type and max length as described <a href="https://mathiasbynens.be/notes/mysql-utf8mb4">here</a>.</p><p>Fortunately, as of MySQL 5.7 things are a bit easier and I’ve included a handy script that will do this for you below. Here’s how it works:</p><p>First, it changes the default character set and collation for the database to utf8mb4 and utf8mb4_unicode_ci, respectively. This tells the database that it should use utf8mb4 as the <strong>default encoding for all new tables.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ii1jPUhoLbFcbWJtzRIk5w.png" /><figcaption>SQL to migrate the database</figcaption></figure><p>Next, it changes the default character set and collation for all existing <strong>tables </strong>and then runs <em>ANALYZE + RECREATE </em>on each. The former will automatically apply the character set and collation to all existing tables and text-based columns/indexes — resizing as needed — while the latter is just <a href="https://dev.mysql.com/doc/refman/5.7/en/analyze-table.html">good practice</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lZ8J_hZ9WStGnOKEquidcA.png" /><figcaption>SQL to migrate the database’s tables (and columns)</figcaption></figure><h3>Bash script to go from utf8 to utf8mb4</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2de53fa651f9d7a37fe33fb285e67812/href">https://medium.com/media/2de53fa651f9d7a37fe33fb285e67812/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41582835dfda676d67c85e61602ec31b/href">https://medium.com/media/41582835dfda676d67c85e61602ec31b/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d8b2753e632" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/nc-django-mysql-migrating-from-utf8-to-utf8mb4-9d8b2753e632">Django + MySQL: Migrating from utf8 to utf8mb4</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>