简体   繁体   English

带有MySQL的Django:DatabaseError(1406,“第1行的列'名称'的数据太长了”)

[英]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.

相关问题 DataError:(1406,“对于第1行的列'name',数据太长”) - DataError: (1406, “Data too long for column 'name' at row 1”) sqlalchemy.exc.DataError: (pymysql.err.DataError) (1406, “第 1 行‘密码’列的数据太长”) - sqlalchemy.exc.DataError: (pymysql.err.DataError) (1406, "Data too long for column 'password' at row 1") mysql.connector.errors.DataError: 1406 (22001): 列的数据太长 - mysql.connector.errors.DataError: 1406 (22001): Data too long for column 数据太长,无法在MySQL上的Django中列 - Data too long for column in Django on MySQL Django 错误 - django.db.utils.DatabaseError:第 1 行“应用”列的数据被截断 - Django Error - django.db.utils.DatabaseError: Data truncated for column 'applied' at row 1 如何处理 (cx_Oracle.DatabaseError) DPI-1037: 数组位置的列提取错误 1406? - How to deal with (cx_Oracle.DatabaseError) DPI-1037: column at array position fetched with error 1406? Django:DatabaseError列不存在 - Django: DatabaseError column does not exist 数据太长Mysql和python - Data too long Mysql and python django模板“文件名太长” - django template “file name too long” Django处理MultipleChoiceField数据花费的时间太长 - Django processing MultipleChoiceField data takes too long
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM