Forums

django.db.utils.OperationalError: (1366, "Incorrect string value: '\\xF0\\x9F\\x93\\xA2 \\xD7' ")

The error below:

django.db.utils.OperationalError: (1366, "Incorrect string value: '\\xF0\\x9F\\x93\\xA2 \\xD7' ")

occurs when using emojis. When I am saving a paragraph containing emojis from the textarea to the database (MySQL). I saw in StackOverflow that to solve this error you need to change the character type of utf8 (which is actually utfmb3 in MySQL) to utfmb4. I am new to MySQL and I don't know how to configure it in the PythonAnywhere. What should I fo, because the use of Emojis are very important. Due to this error, all the characters in the textarea cant be save to the database.

We have a help page for that, see https://help.pythonanywhere.com/pages/DatabaseCharacterSets/.

enter image description here

I tried that as you can see in the image above that "0 rows affected in the TABLE 'posts_postsettings' ". The error is coming in that table only and hasn't been solved from the above two queries.

django.db.utils.OperationalError: (1366, "Incorrect string value: '\\xF0\\x9F\\x93\\xA2 \\xD7...' for column 'post_detail' at row 1")

Let me show you the text I want to write

" "

Between " " I wrote the text but it seems like that text also can't be saved in the PythonAnywhere Database. I have attached a screenshot of the text. The text below is in Hebrew Language


enter image description here

" למכירה סוזוקי אלטו במצב חברה, שמורה מאוד! מנוע 780 סמ"ק שנת 1990, 52k קילומטראז' יד 3, ללא תאונות וריקבונות מנוע אחרי שיפוץ וריפודי מושבים מקוריים ושמורים. קיימות עוד תוספות ברכב עצמו: מערכת שמע, שלטי נוחות וכו'.. מחיר: 12,000 ש"ח, גמיש לרציניים בלבד. מדובר על רכב אספנות! לפרטים - אלי 050-3386068 "

This is the text without emojis. But it saved when I removed the emojis

Do you set anything in your django settings DATABASES['default']['OPTIONS']['charset'] ?

No, nothing like that. But my problem solved. I used this:

ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE table_name MODIFY field_name TEXT CHARSET utf8mb4;

Now, the emojis are getting stored in the database. But after solving that error and trying to save the emojis to the database another error came.

 MySQLdb._exceptions.IntegrityError: (1062, "Duplicate entry '1' for key 'posts_postsettings_group_detail_id_e4449d77_uniq'")

Although this error persists, the emojis are getting stored in the database. The error is due to a problem in my code. But, as I mentioned earlier in the above post that the emojis aren't getting stored when writing these posts. Maybe you didn't set the character set so as to unable the user to add emojis in the forums.

Glad you fixed the emojis. That error is because you're trying to insert a record with a unique field when there is already a record with that value in the database already.