"OperationalError: no such table: django_site" when running makemigrations with empty database when using Django-todo

"OperationalError: no such table: django_site" when running makemigrations with empty database when using Django-todo

我从另一个开发人员那里继承了一个 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。我在 Whosebug 和其他资源上找到的每个解决方案都没有解决问题。

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

回溯显示 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 table 尚未创建。

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