jonalv
jonalv

Reputation: 6116

Django + SQLite how to increase SQLite timeout when "database is locked" error occurs

I am getting the: django.db.utils.OperationalError: database table is locked error (an oh boy are there many copies of that question) all of the answers refer to this page:

https://docs.djangoproject.com/en/dev/ref/databases/#database-is-locked-errorsoption

and although I understand what is going on I clearly don't know Python and Django well enough to understand the instruction. The instruction is to increase the timeout like:

'OPTIONS': {
    # ...
    'timeout': 20,
    # ...
}

but it's not so easy for a-bear-of-very-little-brain to understand exactly where that code goes. Can someone give me a bit more context? Where in my Django project do I specify these sort of options? It can't be a general Django setting can it? Timeout sounds lika a bit too general a name for that...

Upvotes: 7

Views: 6927

Answers (1)

jonalv
jonalv

Reputation: 6116

So, yes it goes in the settings file but not just directly in the settings file but under DATABASES (of course).

My DATABASES part now looks a bit like this:

   DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'OPTIONS': {
            'timeout': 20,  # in seconds
            # see also
            # https://docs.python.org/3.7/library/sqlite3.html#sqlite3.connect
        }
    }

}

Which seems to have done the trick. Maybe this was obvious for everyone else or maybe not. It's not always so easy for a-bear-of-very-little-brain.

Upvotes: 13

Related Questions