Opened 9 years ago

Closed 9 years ago

Last modified 6 years ago

#13919 closed Uncategorized (invalid)

"Incorrect string value" warning when saving some unicode characters to MySQL

Reported by: Denilson Figueiredo de Sá Owned by: nobody
Component: Uncategorized Version: 1.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


How to reproduce this bug, step-by-step:

  1. Create a new project.
  1. Create the database for this project in MySQL. Make sure the database has 'utf8' as its collation.
  1. Create a new app inside your project (here I'm calling it 'testapp'). Remember to add it to
  1. At the models, create a simple one (I've named it TestModel) with just a CharField and/or a TextField.
  1. ./ syncdb
  1. ./ shell
  1. from testapp.models import TestModel
  1. Create a new instance of your model and set its attribute to u"Su\u1296it\U000f2a61\r\n"
  1. Call .save()

Boom! MySQLdb throws:

Warning: Incorrect string value: '\xF3\xB2\xA9\xA1\x0D\x0A' for column 'cf' at row 1

Please note that other unicode strings (like u'Bh\u0101skara\n') do work correctly, thus the MySQL database was set up correctly. However, that string above makes MySQLdb throw an exception.

Does the string above make any sense? I don't know, it was a piece of text copied-and-pasted by the user.


  • Django 1.1.2
  • MySQL 5.0.90
  • mysql-python (MySQLdb) 1.2.3_rc1
  • Python 2.6.5

Change History (3)

comment:1 Changed 9 years ago by Karen Tracey

Resolution: invalid
Status: newclosed

This isn't a Django issue, it's a MySQL one. '\U000f2a61' requires 4 bytes to encode in utf-8, and MySQL only supports up to 3-byte utf-8:

comment:2 Changed 9 years ago by Denilson Figueiredo de Sá

In case someone else hits this bug and wants a solution or workaround, please check this StackOverflow question:

comment:3 Changed 6 years ago by anonymous

Easy pickings: unset
Severity: Normal
Type: Uncategorized
UI/UX: unset

Simple workaround:
After making " syncdb" change CHARACTER SET for 'title' and the 'content' columns by running the following queries:

ALTER TABLE django_flatpage CHANGE COLUMN `title` `title` VARCHAR(200) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NOT NULL;
ALTER TABLE django_flatpage CHANGE COLUMN `content` `content` longtext  CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' NOT NULL;
Note: See TracTickets for help on using tickets.
Back to Top