Forums

mysql data truncated error on django

I've been using an sqlite database for this app i'm writing on top of django and I want to migrate to mysql now that it's starting to accumulate a lot of data.

So i used django's dumpdata to create a json fixture from my sqlite database and tried to load that using django's loaddata into a brand new mysql database and got this error:

mysql_exceptions.Warning: Problem installing fixture '/pahtofixture/fixture.json': Data truncated for column 'name' at row 1

I suspect this is because the data in that column uses Arabic letters.

If you start a MySQL console and run SHOW CREATE TABLE on the table concerned, what charset does it say the table is using?

ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8

It may be that MySQL interprets the length of the field as bytes instead of characters, so try doubling the length of the name field in your model.

Yes, but you may need 3 times the expected max length in characters, see for example: http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html

Quote: To calculate the number of bytes used to store a particular CHAR, VARCHAR, or TEXT column value, you must take into account the character set used for that column and whether the value contains multi-byte characters. In particular, when using the utf8 Unicode character set, you must keep in mind that not all characters use the same number of bytes and can require up to three bytes per character. For a breakdown of the storage used for different categories of utf8 characters, see Section 10.1.10, “Unicode Support”.

I can't believe I didn't catch that. Thank you all!