Django 编程错误 1146 table 不存在
Django programming error 1146 table doesn't exist
我正在新的远程服务器上设置我的 django 项目。当尝试设置数据库 运行ning `python manage.py migrate' 到 运行 all migrations 我得到以下错误:
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
被调用并在 table 存在之前寻找 BlogTag
table。为什么 运行ning 迁移时会发生这种情况?有什么办法可以解决这个问题?
感谢您的帮助。
您的代码编写方式有问题,尤其是这一行:
tag_choices = ((obj.id, obj.tag) for obj in BlogTag.objects.all())
In forms.py : 你不应该在模块主体中使用任何 QuerySet 过滤,因为它是在模块加载时执行的,你宁愿在函数中调用它。
这就是无法应用迁移的原因:您试图在模块 (forms.py) 加载期间使用查询集从数据库中获取数据,但 table 在迁移之前不存在: )
尝试评论这一行并尝试再次应用您的迁移。
但请记住,这是一种不好的做法,如果还有其他类似的行,您最好将它们注释掉以应用迁移,最好将它们移动到函数中
您不应该在模型主体中进行任何 QuerySet 过滤。否则,在导入模块时过滤为 运行。由于导入发生在迁移 运行 之前,这可能会导致解释器尝试调用尚不存在的数据库。这是你的堆栈跟踪的原因,抛出异常 table doesn't exist
.
在模型主体中,最佳做法是将属性设置为 None,然后 运行 您的数据库在 __init__()
方法中调用 class .详情见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 = ...
这确保您的数据库调用仅在创建对象时 运行,而不是在导入 class 或模块时调用。
我正在新的远程服务器上设置我的 django 项目。当尝试设置数据库 运行ning `python manage.py migrate' 到 运行 all migrations 我得到以下错误:
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
被调用并在 table 存在之前寻找 BlogTag
table。为什么 运行ning 迁移时会发生这种情况?有什么办法可以解决这个问题?
感谢您的帮助。
您的代码编写方式有问题,尤其是这一行:
tag_choices = ((obj.id, obj.tag) for obj in BlogTag.objects.all())
In forms.py : 你不应该在模块主体中使用任何 QuerySet 过滤,因为它是在模块加载时执行的,你宁愿在函数中调用它。
这就是无法应用迁移的原因:您试图在模块 (forms.py) 加载期间使用查询集从数据库中获取数据,但 table 在迁移之前不存在: )
尝试评论这一行并尝试再次应用您的迁移。
但请记住,这是一种不好的做法,如果还有其他类似的行,您最好将它们注释掉以应用迁移,最好将它们移动到函数中
您不应该在模型主体中进行任何 QuerySet 过滤。否则,在导入模块时过滤为 运行。由于导入发生在迁移 运行 之前,这可能会导致解释器尝试调用尚不存在的数据库。这是你的堆栈跟踪的原因,抛出异常 table doesn't exist
.
在模型主体中,最佳做法是将属性设置为 None,然后 运行 您的数据库在 __init__()
方法中调用 class .详情见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 = ...
这确保您的数据库调用仅在创建对象时 运行,而不是在导入 class 或模块时调用。