2

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-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' 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?

Alasdair
  • 298,606
  • 55
  • 578
  • 516
saxum
  • 107
  • 2
  • 10
  • 2
    why do you have to do the makemigrations ? don't you have the migrations in your project ? Start by doing ```manage.py migrate```. Looks like you didn't apply the migrations of the installed apps from django – Bestasttung Nov 12 '18 at 15:03
  • Yes that's right, but I got the same error when running `manage.py migrate`. The problem was caused by a bug in django-todo. – saxum Nov 12 '18 at 15:19

1 Answers1

3

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

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\todo\utils.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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • I can't upgrade to 2.0 because it seems to be incompatible with django 1.11.6. But commenting out the todo urls did the trick! Thank you very much. – saxum Nov 12 '18 at 15:17
  • @F.Tamy for this question the problem was in django-todo, not in Django – Alasdair Oct 30 '20 at 20:13
  • @F.Tamy if you find you have to comment out your `urls.py` then there's probably some code imported by `urls.py` that needs to be modified. See [this recent question](https://stackoverflow.com/a/64643510/113962) for example. We're getting off topic here, this question and answer is definitely about a bug in Django-todo, not in Django. – Alasdair Nov 02 '20 at 10:15
  • @F.Tamy we're going round in circles here. I can't see your code or the full traceback, so I can't convince you that there's an issue in your project that you should fix. Running `makemigrations` won't ever solve the problem, you need to run `migrate` to create the table to stop the error. – Alasdair Nov 04 '20 at 09:14
  • @FedericoCapaldo this question and answer are about a specific bug in the `django-todo` app. I don't think your comment is related to that. Your problem will get more visibility if you ask a new question. – Alasdair Mar 17 '21 at 11:06