Using an in-memory sqlite database

Hi all,

in a recent project, using Django 2.2.7, the idea was to use an additional in-memory database, as in this configuration:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'memory': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    }
}

In order to get the memory db initialized, we have this command in the config for one app called memory_db_only:

class MemoryDbOnlyConfig(AppConfig):
    name = 'memory_db_only'

    def ready(self):
        call_command('migrate',
                     app_label='memory_db_only',
                     verbosity=1,
                     interactive=False,
                     database='memory')

When starting a shell (python manage.py shell) and querying some model from the memory db, it works fine:

>>> from memory_db_only.models import MyModel
>>> MyModel.objects.using("memory").all()
<QuerySet []>

However, when we add a view to the memory_db_only app, like

def get_objects(request):
    obj = MyModel.objects.using("memory").all()
    return HttpResponse(str(obj))

an exception is raised:

OperationalError at /memory/objects
no such table: memory_db_only_mymodel

I would highly appreciate advice on how to setup Django in this particular scenario.
Thanks
Christoph

Did you migrate? You’d still need to do that.

Pardon my novice question, but really curious why would you use in-memory database won’t it be a bottleneck?

1 Like

I guess if its a small project and speed is important you could use an in memory DB.

But I think you are right @Devil - I wouldn’t use it on anything with scale.

Having said that SQLite on a SSD is pretty fast, but maybe there are other reasons for using in memory SQLite

Update: Using file::memory: for the name of the memory database fixes the problem.

@ carltongibson: The migrations are run automatically whenever the server restarts, due to the changes to MemoryDbOnlyConfig

@ Devil: In our current project, we would like to use a relational cache, in order to store relational data from an external resource in a non-permanent manner.

@ andyide: Since we only use it for caching limited amounts of data, we don’t expect to see any scalability issues.

1 Like