'Django cache settings blocks all commands

My problem is, that I configured a DB cache for django. Now if I clear the DB, no commands are working:

Traceback (most recent call last):
  File "app/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/apps.py", line 22, in ready
    self.module.autodiscover()
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 23, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/usr/local/lib/python2.7/site-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/site-packages/cms/admin/__init__.py", line 11, in <module>
    plugin_pool.plugin_pool.discover_plugins()
  File "/usr/local/lib/python2.7/site-packages/cms/plugin_pool.py", line 35, in discover_plugins
    invalidate_cms_page_cache()
  File "/usr/local/lib/python2.7/site-packages/cms/views.py", line 340, in invalidate_cms_page_cache
    version = _get_cache_version()
  File "/usr/local/lib/python2.7/site-packages/cms/views.py", line 285, in _get_cache_version
    version = cache.get(CMS_PAGE_CACHE_VERSION_KEY)
  File "/usr/local/lib/python2.7/site-packages/django/core/cache/backends/db.py", line 65, in get
    "WHERE cache_key = %%s" % table, [key])
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "cache_table" does not exist
LINE 1: SELECT cache_key, value, expires FROM "cache_table" WHERE ca...

So obviously the cache_table is missing. But I can't generate a new one with manage.py createcachetable because of this error..

This is the cache config:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'cache_table',
    }
}

Thanks for your help.



Solution 1:[1]

Looking at the traceback, it seems that an app is trying to access the cache table before it is created. You could try temporarily disabling the CMS app until the cache table has been created.

If you can't do that, create a new Django project, use the same database and cache settings, and create the table using the ./manage.py createcachetable command from the new project.

Solution 2:[2]

I ran into the same chicken and egg situation, it seems that if you have set the CACHES setting (even to an empty dictionary) Django CMS tried to access it whenever you run python manage.py ... and therefore you cannot create the cache table.

However createcachetable has a convenient --settings option, and I ended up creating a settings_nocache.py next to my actual settings.py, with this content in it:

from .settings import *
del CACHES

And run the following command to create my cache table:

python manage.py createcachetable --settings settings_nocache default_cache

The argument after the --settings option is the python import path, not the filesystem path.

Solution 3:[3]

My problem had to do with pytest.

So I put the following in conftest.py:

@pytest.fixture(scope="session")
def django_db_setup():
    del settings.CACHES

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alasdair
Solution 2
Solution 3 Dash2TheDot