Search code examples
djangodjango-modelsdjango-migrations

OperationalError: no such table but database and migrations folder were deleted


Very similar to this question:

I have a script that:

  • dumps all data to json,
  • deletes the database.sqlite3 file and the migrations folder
  • executes:
    1. python makemigrations app
    2. python manage.py migrate app
    3. python manage.py makemigrations
    4. python manage.py migrate
  • loads all data from json

I use it a lot when developing stuff and restarting very often. Now even the makemigrations app command fails with the OperationalError: no such table.

Funny thing is, when I comment out all appearances of this table (but the model is still in models.py), everything works fine. If I then delete the comments everything works. What did I miss?

Traceback:

PS ..\projectname> python manage.py migrate appname       
Traceback (most recent call last):
  File "..\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "..\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: appname_unmigrateablemodel

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

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "..\venv\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "..\venv\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "..\venv\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "..\venv\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "..\venv\lib\site-packages\django\core\management\base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "..\venv\lib\site-packages\django\core\management\commands\migrate.py", line 75, in handle
    self.check(databases=[database])
  File "..\venv\lib\site-packages\django\core\management\base.py", line 419, in check
    all_issues = checks.run_checks(
  File "..\venv\lib\site-packages\django\core\checks\registry.py", line 76, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "..\venv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "..\venv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "..\venv\lib\site-packages\django\urls\resolvers.py", line 412, in check
    for pattern in self.url_patterns:
  File "..\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "..\venv\lib\site-packages\django\urls\resolvers.py", line 598, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "..\venv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "..\venv\lib\site-packages\django\urls\resolvers.py", line 591, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "..\projectname\projectname\urls.py", line 24, in <module>
    path('', include('appname.urls')), ## must be the last ! line in the URLS file because of the empty argument #
  File "..\venv\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "..\projectname\appname\urls.py", line 2, in <module>
    from . import views
  File "..\projectname\appname\views.py", line 15, in <module>
    from .external.server_api import APISERVICE
  File "..\projectname\appname\external\hiveserver_api.py", line 10, in <module>
    USB_ETHERNET_ADAPTER_MAC_LIST = [A.mac for A in unmigrateablemodel.objects.all()]
  File "..\venv\lib\site-packages\django\db\models\query.py", line 280, in __iter__
    self._fetch_all()
  File "..\venv\lib\site-packages\django\db\models\query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "..\venv\lib\site-packages\django\db\models\query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "..\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "..\venv\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "..\venv\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "..\venv\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "..\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "..\venv\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "..\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "..\venv\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: appname_unmigrateablemodel

Solution

  • This part in your traceback:

    File "..\projectname\appname\views.py", line 15, in <module>
        from .external.server_api import APISERVICE
    File "..\projectname\appname\external\hiveserver_api.py", line 10, in <module>
        USB_ETHERNET_ADAPTER_MAC_LIST = [A.mac for A in unmigrateablemodel.objects.all()]
    

    tells us you're doing a query during import time of the hiveserver_api.py module.

    Since there are no tables in an empty database, importing that module will always fail, and it's imported via views, which is imported via urls, which is imported since the app is in INSTALLED_APPS.

    You will need to refactor that code so it doesn't do that query at import time, or just temporarily:

    USB_ETHERNET_ADAPTER_MAC_LIST = []
    

    This also implies there's a bug in your application; that list of MAC addresses will never be updated in your views until/unless the application is reloaded.


    As an aside,

    USB_ETHERNET_ADAPTER_MAC_LIST = list(
        unmigrateablemodel.objects.all().values_list("mac", flat=True)
    )
    

    is the efficient way to do the same thing.