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

I have inherited a Django 1.11.6 application from another developer. Now after I have made a lot of changes to the code and data model, I want to re-create the sqlite database from scratch.

But when I run manage.py makemigrations in the project directory (no migration files and database present), I am getting the following error message:

C:Django_DEV>python manage.py makemigrations --settings=mysite.settings_dev
Traceback (most recent call last):
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbbackendsutils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbbackendssqlite3base.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-32libsite-packagesdjangocoremanagement__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "C:Program Files (x86)Python36-32libsite-packagesdjangocoremanagement__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangocoremanagementbase.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangocoremanagementbase.py", line 327, in execute
    self.check()
  File "C:Program Files (x86)Python36-32libsite-packagesdjangocoremanagementbase.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "C:Program Files (x86)Python36-32libsite-packagesdjangocoremanagementbase.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangocorechecksregistry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangocorechecksurls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangocorechecksurls.py", line 26, in check_resolver
    return check_method()
  File "C:Program Files (x86)Python36-32libsite-packagesdjangourlsresolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "C:Program Files (x86)Python36-32libsite-packagesdjangoutilsfunctional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangourlsresolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangoutilsfunctional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangourlsresolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:Program Files (x86)Python36-32libimportlib__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_DEVmysiteurls.py", line 29, in <module>
    url(r'^todo/', include('todo.urls')),
  File "C:Program Files (x86)Python36-32libsite-packagesdjangoconfurls__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "C:Program Files (x86)Python36-32libimportlib__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-32libsite-packagestodourls.py", line 2, in <module>
    from todo import views
  File "C:Program Files (x86)Python36-32libsite-packagestodoviews.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-32libsite-packagestodoutils.py", line 10, in <module>
    current_site = Site.objects.get_current()
  File "C:Program Files (x86)Python36-32libsite-packagesdjangocontribsitesmodels.py", line 63, in get_current
    return self._get_site_by_id(site_id)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangocontribsitesmodels.py", line 35, in _get_site_by_id
    site = self.get(pk=site_id)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbmodelsmanager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbmodelsquery.py", line 374, in get
    num = len(clone)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbmodelsquery.py", line 232, in __len__
    self._fetch_all()
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbmodelsquery.py", line 1118, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbmodelsquery.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbmodelssqlcompiler.py", line 894, in execute_sql
    raise original_exception
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbmodelssqlcompiler.py", line 884, in execute_sql
    cursor.execute(sql, params)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbbackendsutils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbbackendsutils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbutils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangoutilssix.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbbackendsutils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:Program Files (x86)Python36-32libsite-packagesdjangodbbackendssqlite3base.py", line 328, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: django_site

'django.contrib.sites' is present in the INSTALLED_APPS and there is a SITE_ID defined. Every solution I found on stackoverflow and other sources didn’t fix the problem.

How can I get past this and continue developing the application with a empty database?

Answer

The traceback shows that todoutils.py is trying to fetch the current site from the database when the URL patterns are loaded:

  File "C:Program Files (x86)Python36-32libsite-packagestodoutils.py", line 10, in <module>
current_site = Site.objects.get_current()

When this happens before you have run migrate for the first time, you get the error because the django_site table has not been created yet.

It looks as if the issue has been fixed in django-todo 2.0. If you can’t upgrade, then as a hack you could temporarily comment out the django-todo urls.py or the current_site = Site.objects.get_current() line, so that you can run migrate.

Leave a Reply

Your email address will not be published. Required fields are marked *