[英]Django with MySQL: DatabaseError (1406, “Data too long for column 'name' at row 1”)
I have a Django webapp, currently been testing with SQLite but now want to deploy and use MySQL, and I'm getting this error. 我有一个Django webapp,目前正在使用SQLite进行测试,但现在想要部署和使用MySQL,我收到了这个错误。
I am getting this error right when using python manage.py syncdb
: 我在使用
python manage.py syncdb
时遇到此错误:
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): no
DatabaseError: (1406, "Data too long for column 'name' at row 4")
And also when trying to create a Store
object (one of the models) with this code: 并且在尝试使用以下代码创建
Store
对象(其中一个模型)时:
store_leicester = Store.objects.create(
name='Hugo Boss UK Store Leicester Square',
country='United Kingdom',
district='London',
catalog=catalog ...
)
Error: 错误:
DatabaseError at /populate/
(1406, "Data too long for column 'name' at row 1")
Store model, for example, is: 例如,商店模型是:
class Store(models.Model):
"""
Class for a Store.
"""
name = models.TextField(max_length=128)
country = models.TextField(max_length=64)
district = models.TextField(max_length=64)
catalog = models.OneToOneField('ShopCatalog', related_name='shop', null=True)
chain = models.ForeignKey('StoreChain', related_name="shops", null=True)
Now naturally, these 10-15 characters of text are not above the 128 character limit, so there is something else going on. 现在很自然地,这些10-15个字符的文本不超过128个字符的限制,所以还有其他的东西在继续。 Starting by the fact the error also comes up on syncdb.
从syncdb上出现错误的事实开始。
I'm using two Django packages I created myself that use models, but I don't think that is the problem here. 我正在使用我自己创建的两个使用模型的Django包,但我认为这不是问题所在。
Default collation on schema was latin-1
but I tried switching to utf-8
and still exactly same error. 架构上的默认排序规则是
latin-1
但我尝试切换到utf-8
但仍然完全相同的错误。
Thank you 谢谢
When you syncdb
, Django also stores the verbose name of your model in its internal django_content_type
table. 当您使用
syncdb
,Django还会将模型的详细名称存储在其内部的django_content_type
表中。 That table has a limit of (as of Django 1.2) 100 characters. 该表的限制为(自Django 1.2起)100个字符。 Having a model whose verbose name is over 100 characters will cause the problem you are experiencing.
拥有详细名称超过100个字符的模型将导致您遇到的问题。
Check if your app has any other models with a long name/ verbose_name
, and try shortening them. 检查您的应用是否包含任何其他具有长名称/
verbose_name
模型,并尝试缩短它们。 That should fix the problem. 这应该解决问题。 (
Store
doesn't seem long at all, but perhaps there's a different model you didn't mention.) (
Store
似乎没有多长时间,但也许有一个你没有提到的不同模型。)
By the way, the reason you didn't see this with SQLite is that if I remember correctly SQLite doesn't enforce length restrictions on char fields. 顺便说一句,你没有看到SQLite的原因是,如果我没记错,SQLite不会对char字段强制执行长度限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.