使用 Django-todo 时使用空数据库运行 makemigrations 时出现“OperationalError:没有这样的表:django_site”

Posted

技术标签:

【中文标题】使用 Django-todo 时使用空数据库运行 makemigrations 时出现“OperationalError:没有这样的表:django_site”【英文标题】:"OperationalError: no such table: django_site" when running makemigrations with empty database when using Django-todo 【发布时间】:2019-04-15 07:35:25 【问题描述】:

我从另一个开发人员那里继承了一个 Django 1.11.6 应用程序。现在我对代码和数据模型做了很多修改后,我想从头开始重新创建 sqlite 数据库。

但是当我在项目目录中运行manage.py makemigrations 时(不存在迁移文件和数据库),我收到以下错误消息:

C:\Django_DEV>python manage.py makemigrations --settings=mysite.settings_dev
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: django_site

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 327, in execute
    self.check()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
    return check_method()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\urls\resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\urls\resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\urls\resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Program Files (x86)\Python36-32\lib\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 "C:\Django_DEV\mysite\urls.py", line 29, in <module>
    url(r'^todo/', include('todo.urls')),
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\conf\urls\__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Program Files (x86)\Python36-32\lib\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 "C:\Program Files (x86)\Python36-32\lib\site-packages\todo\urls.py", line 2, in <module>
    from todo import views
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\todo\views.py", line 20, in <module>
    from todo.utils import mark_done, undo_completed_task, del_tasks, send_notify_mail
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\todo\utils.py", line 10, in <module>
    current_site = Site.objects.get_current()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\contrib\sites\models.py", line 63, in get_current
    return self._get_site_by_id(site_id)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\contrib\sites\models.py", line 35, in _get_site_by_id
    site = self.get(pk=site_id)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\query.py", line 374, in get
    num = len(clone)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\query.py", line 232, in __len__
    self._fetch_all()
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\query.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\sql\compiler.py", line 894, in execute_sql
    raise original_exception
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\models\sql\compiler.py", line 884, in execute_sql
    cursor.execute(sql, params)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\utils\six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: django_site

'django.contrib.sites' 存在于 INSTALLED_APPS 中,并且定义了 SITE_ID。我在 *** 和其他来源找到的每个解决方案都没有解决问题。

我怎样才能克服这个问题并继续使用空数据库开发应用程序?

【问题讨论】:

你为什么要做makemigrations?您的项目中没有迁移吗?从manage.py migrate 开始。看起来您没有从 django 应用已安装应用程序的迁移 是的,没错,但是我在运行manage.py migrate 时遇到了同样的错误。该问题是由 django-todo 中的错误引起的。 【参考方案1】:

回溯显示todo\utils.py 在加载 URL 模式时试图从数据库中获取当前站点:

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\todo\utils.py", line 10, in <module>
current_site = Site.objects.get_current()

如果在您第一次运行 migrate 之前发生这种情况,您会收到错误,因为尚未创建 django_site 表。

看起来好像是 django-todo 2.0 中的问题has been fixed。如果你不能升级,那么作为一个hack,你可以暂时注释掉django-todo urls.py 或current_site = Site.objects.get_current() 行,这样你就可以运行migrate

【讨论】:

我无法升级到 2.0,因为它似乎与 django 1.11.6 不兼容。但是注释掉待办事项网址就可以了!非常感谢。 @F.Tamy 对于这个问题,问题出在 django-todo 中,而不是 Django 中 @F.Tamy 如果您发现必须注释掉您的urls.py,那么可能有一些由urls.py 导入的代码需要修改。例如,请参阅this recent question。我们在这里偏离主题,这个问题和答案肯定是关于 Django-todo 中的一个错误,而不是 Django 中的一个错误。 @F.Tamy 我们在这里绕圈子。我看不到您的代码或完整的回溯,所以我无法让您相信您的项目中存在您应该修复的问题。运行makemigrations 永远不会解决问题,您需要运行migrate 来创建表以停止错误。 @FedericoCapaldo 这个问题和答案是关于django-todo 应用程序中的一个特定错误。我认为您的评论与此无关。如果您提出新问题,您的问题会得到更多关注。

以上是关于使用 Django-todo 时使用空数据库运行 makemigrations 时出现“OperationalError:没有这样的表:django_site”的主要内容,如果未能解决你的问题,请参考以下文章

使用 Spring MVC 运行 Spring Boot 时引发空指针异常

无法对空引用执行运行时绑定,但它不是空引用

在使用 java 运行 Hadoop map reduce 作业时抛出空指针异常

使用TestNG运行代码时出现空指针异常

使用 OCUnit 进行单元测试时的 NSBundle 返回(空)

连接到数据库时无法读取属性“运行”的空错误