我有一个 Django 网络应用程序,目前正在使用 SQLite 进行测试,但现在想部署和使用 MySQL,但我遇到了这个错误。
我在使用 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")
并且在尝试使用此代码创建 Store
对象(模型之一)时:
store_leicester = Store.objects.create(
name='Hugo Boss UK Store Leicester Square',
country='United Kingdom',
district='London',
catalog=catalog ...
)
错误:
DatabaseError at /populate/
(1406, "Data too long for column 'name' at row 1")
例如商店模型是:
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)
自然地,这 10-15 个字符的文本不超过 128 个字符的限制,所以还有其他事情要发生。首先是错误也出现在 syncdb 上。
我正在使用我自己创建的两个使用模型的 Django 包,但我认为这不是这里的问题。
模式的默认排序规则是latin-1
,但我尝试切换到utf-8
,但仍然出现完全相同的错误。
谢谢
最佳答案
当您syncdb
时,Django 还会将模型的详细名称存储在其内部django_content_type
表中。该表的限制为(从 Django 1.2 开始)100 个字符。模型的详细名称超过 100 个字符会导致您遇到问题。
检查您的应用是否有任何其他名称/verbose_name
很长的模型,并尝试缩短它们。那应该可以解决问题。 (Store
看起来一点也不长,但也许还有您没有提到的不同模型。)
顺便说一句,您没有在 SQLite 中看到这一点的原因是,如果我没记错的话,SQLite 不会对 char 字段强制执行长度限制。
关于python - Django with MySQL : DatabaseError (1406, "Data too long for column ' name' at row 1"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16309961/