[SOLVED] django.db.utils.OperationalError: (1366, "Incorrect string value) django mysql

Issue

hi i created a django project and tried to deploy it on ubuntu apache server with mysql database

but when i run python3 manage.py migrate i get this error :

django.db.utils.OperationalError: (1366, "Incorrect string value: ‘\xD9\x88\xDB\x95\xD8\xB3…’ for column ‘name’ at row 1")

i have only one column named name = models.CharField(max_length=30)

when i run show variables like 'char%'; the output is :

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

and in my /etc/mysql/my.cnf i set [client] default-character-set = utf8
is there something i’ve missed ?!

Solution

The “utf8” encoding only supports three bytes per character so, you cannot store 4-byte characters in MySQL with the utf-8 character set.

For more info read this

1.Change your MySQL database, table and columns to use the utf8mb4 character set

2.In your settings.py

DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.mysql',
        ...
        'OPTIONS': {'charset': 'utf8mb4'},
    }
}

Answered By – Sachin Yadav

Answer Checked By – Senaida (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *