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

时间:2018-11-12 14:46:05

标签: python django sqlite django-migrations django-1.11

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

如何克服这个问题,并继续使用空数据库开发应用程序?

1 个答案:

答案 0 :(得分:1)

追溯显示//initial API call var request = new XMLHttpRequest(); request.open('GET', apiUrl + apiRequest + apiCredential + apiParameter + apiFilter, true); request.onload = function () { //accessing JSON data data = this.response; if (request.status >= 200 && request.status < 400) { document.getElementById('test').innerHTML = data.CreatedBy; } } request.send(); 正在尝试在加载URL模式时从数据库中获取当前站点:

todo\utils.py

在第一次运行 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-todo 2.0中的问题has been fixed。如果您无法升级,那么作为黑客,您可以暂时将django-todo urls.py或django_site行注释掉,以便您可以运行current_site = Site.objects.get_current()