Django inserting the string "(NULL)" to MySQL fields for Null instead of NULL
I'm trying to use a 2 character field that is nullable for an address table in MySQL. When I run my manage.py syncdb command, it loads my data into the MySQL table, but yields the error:
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue DatabaseError: (1406, "Data too long for column 'state_province' at row 1")
I found开发者_JAVA技巧 the issue to be the Django is inserting into the table the string "(NULL)" instead of the NULL value for SQL.
The field in question is "state_province" which is null=True, blank=True in the model definition, with a length of 2.
Here is a sample set of data:
- model: myapp.Address
pk: 53
fields:
street_address: "71 South St"
city: cityname
state_province:
zip_code: 25455
countryID: 199
time_zone: -6
- model: myapp.Address
pk: 54
fields:
street_address: "123 lake street"
city: Townname
state_province: NH
zip_code: 12345
countryID: 199
time_zone: -5
EDIT: I forgot to link, this question is actually exactly what is happening, but I don't know how to correct this behavior in Django, or where to start: Inserting null into char(1)
Well, it may sound harsh, but this is a core and very high profile part of Django's inner workings. If it was a bug in Django, it would be well-documented and, more importantly, fixed already.
Since you're experiencing this error with manage.py and not your own code, though, my best guess is that the field is not actually set to allow NULL on the MySQL table, and Django is trying to compensate the best it can.
Check your MySQL table and ensure that NULL is allowed on the state_province
column.
精彩评论