使用django的manage.py脚本同步db时的mysql异常

时间:2010-10-06 22:09:45

标签: django django-models

我对django比较新。我已经定义了我的数据库模式并验证了它没有错误(manage.py验证报告找到0错误)。

然而,当我运行./manage.py syncdb

我得到以下堆栈跟踪:

Creating table demo_foobar_one
Creating table demo_foobar_two
<snip>...</snip>
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 218, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 347, in handle
    return self.handle_noargs(**options)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/syncdb.py", line 103, in handle_noargs
    emit_post_sync_signal(created_models, verbosity, interactive, db)
  File "/usr/local/lib/python2.6/dist-packages/django/core/management/sql.py", line 185, in emit_post_sync_signal
    interactive=interactive, db=db)
  File "/usr/local/lib/python2.6/dist-packages/django/dispatch/dispatcher.py", line 162, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/management/__init__.py", line 28, in create_permissions
    defaults={'name': name, 'content_type': ctype})
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 135, in get_or_create
    return self.get_query_set().get_or_create(**kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 373, in get_or_create
    obj.save(force_insert=True, using=self.db)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 435, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 528, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 1479, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 783, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)
  File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 727, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 15, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.6/dist-packages/django/db/backends/mysql/base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python2.6/dist-packages/MySQL_python-1.2.3-py2.6-linux-i686.egg/MySQLdb/cursors.py", line 176, in execute
    if not self._defer_warnings: self._warning_check()
  File "/usr/local/lib/python2.6/dist-packages/MySQL_python-1.2.3-py2.6-linux-i686.egg/MySQLdb/cursors.py", line 92, in _warning_check
    warn(w[-1], self.Warning, 3)
_mysql_exceptions.Warning: Data truncated for column 'name' at row 1

我检查过(并仔细检查过)我的表架构。所有名称字段都是CharField类型,最大长度= 64.我使用的后端数据库是MySQL,因此我确信可以为长度为64的字符串创建索引。

可能导致此错误的原因(我怀疑这是一个误导性错误消息 - 即使它来自数据库本身)...

3 个答案:

答案 0 :(得分:5)

在创建django.contrib.auth.Permission期间会发生回溯:其中一些会自动作为syncdb的一部分为您的模型创建。

Permission.namemax_length=50,所以你可能有一个名字很长的应用程序和/或模型类?

manage.py dbshell中尝试以下查询:

SELECT * FROM auth_permission WHERE LENGTH(name) = 50;

如果您无法更改模型名称,则可以通过在模型Meta类中指定Permission.name来缩短生成的verbose_name的长度来解决此问题(有关详细信息,请参阅here详情):

class MyVeryLongModelNameThatIsBreakingMyMigration(models.Model):
    class Meta:
        verbose_name = 'my long model'

更新

有一个开放的(截至2013年)Django门票来解决这个问题:

答案 1 :(得分:3)

正如Piet Delport所指出的,the problem is that your model name is too long

您肯定必须缩短模型名称,然后清理数据库。你如何做到这一点取决于你在开发过程中的位置。

  • 如果这是一个全新的应用程序,具有专用数据库,并且没有实际数据,那么简单的答案是删除并重新创建数据库,然后重新运行python manage.py syncdb

  • 如果数据库中有其他表需要单独使用,但Django的表没有“真实”数据,因此可以在没有损坏的情况下删除,那么您可以使用{{1}生成SQL DDL以删除所有Django生成的表,约束和索引。

    执行以下操作:

    manage.py sqlclear

    您可以将生成的脚本提供给apps="auth contenttypes sessions sites messages admin <myapp1> <myapp2>" python manage.py sqlclear ${apps} > clear.sql mysql。完成后,您可以重新运行python manage.py dbshell

  • 如果您的数据库表中有实际数据无法删除或删除:自己轻拍并重复100次“我绝不会对生产数据库进行开发在更改之前,我将始终备份我的数据库。“现在您将不得不手动编写SQL来更改受影响的表名,以及引用它的任何其他内容和< / strong> python manage.py syncdb表和任何其他Django系统表中的任何引用。您的实际步骤将完全取决于数据库和表的状态。

答案 2 :(得分:1)

我也使用postgresql django 1.2这样的错误,但问题不是长度,而是使用ugettext_lazy来翻译名称。 ('can_purge', _("Can purge"))显然是不可接受的,因为名称作为文本存储在数据库中