2

我正在拔头发。我只是不能让迁移工作了。每次我运行python3 manage.py makemigrationspython3 manage.py makemigrations app_name我收到以下错误:

    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 和其他网站上阅读了很多问答,但无法解决问题。

我已经尝试按照以下已接受答案中的步骤进行操作,但没有区别:Django - no such table exception

我已经尝试解决这个问题好几个小时了,但我还是无处可去。

感谢您的帮助!

4

3 回答 3

2

哇,经过几个小时无处可去,我真的刚刚解决了它!

原来我试图在urls.py数据库准备好之前将数据加载到 sqlite 数据库中。

makemigrations在执行之前触发了loader.load_db()呼叫migrate。一旦我注释掉loader.load_db()迁移就完美了!:)

因此,总而言之,请确保您在执行之后才尝试将任何数据加载到数据库makemigrationsmigrate

于 2020-02-28T12:30:11.727 回答
1

您在模块级别上有代码,该代码在堆栈的下方执行了一个查询:

fault = Fault.objects.get(fault__icontains = fault_name)

在模块级别,此代码将在分析模块时执行,makemigrations结果查询将失败,因为尚未创建数据库表。

作为一种解决方案,您必须将该代码放置在导入时不会执行的位置,否则您必须处理引发的异常。

于 2020-02-28T12:31:10.983 回答
0

当我尝试将基于函数的视图更改为基于类的视图时,我遇到了同样的问题。我尝试通过调用数据库查询来分配类变量,以便它在创建实际表之前尝试进行数据库调用,在删除此分配之后makemigrations效果很好。

这是我的代码示例,当我发现这个错误时

class SampleView(LoginRequiredMixin, UpdateView):
    sample_variable = SampleModel.objects.get(active=True)

我解决这个问题的方式

class SampleView(LoginRequiredMixin, UpdateView):
    sample_variable = None

    def get_object(self, queryset=None):
        self.sample_variable = SampleModel.objects.get(active=True)
        return super(SampleView, self).get_object(queryset)

如果您遇到这样的错误,请查看您的观点,可能是同一个错误导致了您的代码中的错误。

在我的情况下,我将数据库查询和变量分配移动到覆盖的 get 方法,并在初始化时将变量保留为None值

PS我覆盖get_object了这个例子,但你可以将变量的赋值移动到一些不同的函数

于 2021-03-14T12:37:54.093 回答