Django 插入字符串“(NULL)” MySQL 字段为 Null 而不是 NULL

发布于 2024-11-06 07:53:23 字数 1033 浏览 0 评论 0原文

我正在尝试使用 MySQL 中的地址表可以为空的 2 个字符字段。当我运行manage.pysyncdb命令时,它将我的数据加载到MySQL表中,但产生错误:

File“C:\Python27\lib\site-packages\MySQLdb\connections.py”, line 36, in defaulterrorhandler raise errorclass, errorvalue DatabaseError: (1406, "Data too long for columns 'state_province' at row 1")

我发现问题是 Django 正在将字符串插入到表中“(NULL)”而不是 SQL 的 NULL 值。

有问题的字段是“state_province”,模型定义中为 null=True,blank=True,长度为 2。

这是一个示例数据集:

 - 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

编辑:我忘了链接,这个问题实际上是什么正在发生,但我不知道如何在 Django 中纠正这种行为,或者从哪里开始: 将 null 插入 char(1)

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 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2024-11-13 07:53:23

嗯,这可能听起来很刺耳,但这是 Django 内部运作的核心且非常引人注目的部分。如果这是 Django 中的一个错误,那么它会被详细记录,更重要的是,它已经被修复了。

不过,由于您在manage.py而不是您自己的代码中遇到了此错误,所以我最好的猜测是该字段实际上并未设置为允许MySQL表上的NULL,并且Django正在尽力补偿。

检查您的 MySQL 表并确保 state_province 列允许 NULL。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文