简体   繁体   English

Django 2.2 - django.db.utils.OperationalError: 没有这样的表

[英]Django 2.2 - django.db.utils.OperationalError: no such table

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:每次运行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

I have read lots of Q&As on Stackoverflow and other sites but just can't solve the problem.我在 Stackoverflow 和其他网站上阅读了很多问答,但无法解决问题。

I've tried following the steps in the following accepted answer but it makes no difference: Django - no such table exception我已经尝试按照以下接受的答案中的步骤进行操作,但没有任何区别: Django - no such table exception

I've been trying to fix this problem for hours and I'm just not getting anywhere.几个小时以来,我一直在努力解决这个问题,但我一无所获。

Thank you for your help!感谢您的帮助!

Wow, after hours of getting nowhere, I've literally just solved it!哇,经过几个小时的无处可去,我真的刚刚解决了它!

Turns out I was trying to load data into the sqlite database in urls.py before the database was ready.结果我试图在数据库准备好之前将数据加载到urls.py的 sqlite 数据库中。

makemigrations triggered the loader.load_db() call before migrate was performed. makemigrations在执行migrate之前触发了loader.load_db()调用。 Once I commented out loader.load_db() the migrations worked perfectly!一旦我注释掉loader.load_db()迁移工作完美! :) :)

So in summary, make sure you're not trying to load any data into the database until after makemigrations and migrate have been performed.所以总而言之,请确保在执行makemigrationsmigrate之前,您不会尝试将任何数据加载到数据库中。

You are having code in on module level that executed a query further down the stack:您在模块级别上有代码,可以在堆栈中进一步执行查询:

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

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.在模块级别,当makemigrations分析模块时将执行此代码,并且由于尚未创建数据库表,因此结果查询将失败。

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.作为解决方案,您必须将该代码放置在不会在导入时执行的位置,或者您必须处理引发的异常。

I have faced the same problem when I was trying to change function-based views to class-based views.当我尝试将基于函数的视图更改为基于类的视图时,我遇到了同样的问题。 I tried to assign class variables by calling database query, so that it was trying to make database calls before creating actual tables, after removing this assignment makemigrations worked perfectly.我尝试通过调用数据库查询来分配类变量,以便它在创建实际表之前尝试进行数据库调用,在删除此分配后, makemigrations工作得很好。

Here is how my code example, when I was catching this error这是我的代码示例,当我捕获此错误时

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

The way I resolved this problem我解决这个问题的方式

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)

If you're facing such an error, check out your views, probably the same mistake caused the error in your code.如果您遇到这样的错误,请查看您的视图,可能是同样的错误导致了代码中的错误。

In my case, I moved database query and variable assignment to overridden get method and left the variable with None value on initialization就我而言,我将数据库查询和变量赋值移动到覆盖 get 方法,并在初始化时将变量保留为None

PS i override get_object for this example, but you can move assignment of the variable to some distinct function PS我在这个例子中覆盖了get_object ,但是你可以将变量的赋值移动到一些不同的函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM