问题描述
我正在拔头发.我只是不能让迁移工作了.每次我运行 python3 manage.py makemigrations
或 python3 manage.py makemigrations app_name
时,都会出现以下错误:
I am pulling my hair out. I just can't get migrations to work anymore. Every time I run python3 manage.py makemigrations
or python3 manage.py makemigrations app_name
I get the following error:
Traceback (most recent call last):
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: catalog_fault
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/base.py", line 361, in execute
self.check()
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
all_namespaces = _load_all_namespaces(resolver)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
url_patterns = getattr(resolver, 'url_patterns', [])
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 584, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 577, in urlconf_module
return import_module(self.urlconf_name)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/peter/Documents/21q/locallibrary/urls.py", line 60, in <module>
loader.load_db()
File "/home/peter/Documents/21q/catalog/loader.py", line 106, in load_db
add_game(*game, batch=True)
File "/home/peter/Documents/21q/catalog/models.py", line 315, in add_game
fault = add_fault(fault_name, batch=batch)
File "/home/peter/Documents/21q/catalog/models.py", line 327, in add_fault
fault = Fault.objects.get(fault__icontains = fault_name)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/query.py", line 402, in get
num = len(clone)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/query.py", line 256, in __len__
self._fetch_all()
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1133, in execute_sql
cursor.execute(sql, params)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
return super().execute(sql, params)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/home/peter/.virtualenvs/21q_env/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: catalog_fault
我已经在Stackoverflow和其他站点上阅读了很多问答,但无法解决问题.
I have read lots of Q&As on Stackoverflow and other sites but just can't solve the problem.
我已尝试按照以下接受的答案中的步骤进行操作,但这没有什么区别: Django-没有此类表例外
I've tried following the steps in the following accepted answer but it makes no difference: Django - no such table exception
我已经尝试解决这个问题了好几个小时,但是我什么都没得到.
I've been trying to fix this problem for hours and I'm just not getting anywhere.
谢谢您的帮助!
推荐答案
您在模块级别上的代码已在堆栈的更下方执行了查询:
You are having code in on module level that executed a query further down the stack:
fault = Fault.objects.get(fault__icontains = fault_name)
在模块级别,当通过 makemigrations
分析模块时,将执行此代码,并且由于尚未创建数据库表,因此导致查询失败.
On module level this code will be executed when the module is analyzed by makemigrations
and the resulting query will fail since the database table is not created yet.
作为解决方案,您必须将该代码放置在导入时不会执行的位置,或者必须处理引发的异常.
As a solution you will have to place that code at a location where it will not be executed on import or you have to handle the exception raised.
这篇关于Django 2.2-django.db.utils.OperationalError:没有这样的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!