Django编程错误1146表不存在

时间:2015-03-27 20:59:43

标签: python mysql django database-migration

我在新的远程服务器上设置我的django项目。当试图设置运行`python manage.py migrate'的数据库时要运行所有迁移,我收到以下错误:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/apps.py", line 22, in ready
    self.module.autodiscover()
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py", line 23, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/django/kwp/app/admin.py", line 3, in <module>
    from app.views import genCustCode
  File "/home/django/kwp/app/views.py", line 6, in <module>
    from app.forms import *
  File "/home/django/kwp/app/forms.py", line 466, in <module>
    tag_choices = ((obj.id, obj.tag) for obj in BlogTag.objects.all())
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 141, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 265, in iterator
    for row in compiler.results_iter():
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 700, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 128, in execute
    return self.cursor.execute(query, args)
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.ProgrammingError: (1146, "Table 'kwp_db_prod.app_blogtag' doesn't exist")

我不确定问题是什么,但看起来我的forms.py被调用并在该表存在之前查找BlogTag表。为什么在运行迁移时会发生这种情况?有办法解决这个问题吗?

感谢您的帮助。

2 个答案:

答案 0 :(得分:15)

编写代码的方式存在问题,尤其是这一行:

tag_choices = ((obj.id, obj.tag) for obj in BlogTag.objects.all())

在forms.py中:你不应该在模块体中使用任何QuerySet过滤,因为它是在模块加载时执行的,你宁愿在函数中调用它。

这就是为什么无法应用迁移的原因:您在模块(forms.py)加载期间尝试使用queryset从数据库获取数据,但在迁移之前表格不存在: )

尝试对此行发表评论,然后尝试再次应用迁移。

但请记住,这是一种不好的做法,如果有其他类似的行,你最好评论它们来应用迁移,最好将它们转移到函数中

答案 1 :(得分:0)

您不应该在模型主体中进行任何 QuerySet 过滤。否则在导入模块时运行过滤。由于导入在运行迁移之前发生,这可能会导致解释器尝试调用尚不存在的数据库。这是堆栈跟踪的原因,抛出异常 table doesn't exist

在模型主体中,最佳做法是将属性设置为 None,然后在该类的 __init__() 方法中运行数据库调用。有关详细信息,请参阅 here

class FooMultipleChoiceForm(forms.Form):
    foo_select = forms.ModelMultipleChoiceField(queryset=None)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['foo_select'].queryset = ...

这可确保您的数据库调用仅在创建对象时运行,而不是在导入类或模块时运行。