Using django with an in-memory sqlite database

Hi, I’ve been trying to use Django with an in memory sqlite database. I basically have no custom models, I need it for the default django models of django_background_tasks and others. Although the server starts, migrations do not seem to be applied, even though I do it manually before the server runs, and even overriding the app initialization in AppConfig.

This is my database configuration:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": ":memory:",  # I've also tried file::memory: and file::memory:?cache=shared
    }
}

Any help would be appreciated, thank you.

Yes, most likely the migrations are getting applied. However, since that’s a different process than your server, the database created and migrated ceases to exist when the migration process ends. Your server then starts by creating a new database.

I really don’t think you’re going to be able to get this to work with your default database.

You might not, but I guarantee that Django defines models.

(I can’t possibly imagine what your environment would be that can’t afford the space of a minimal sqlite database for deployment purposes, or what the effects of doing this are going to be in a production environment.)

On second thought, you might be able to create a parent process for your Django server that creates the database - then runs the migrate command and your server process, somehow passing the virtual file handle through to each - but I have no idea what such a parent process might look like.

Or, it may be easier just to define a “RAM drive” in your Linux environment, and put the sqlite database on it. At least that way, it’ll last as long as your server remains running.

Blockquote
Yes, most likely the migrations are getting applied. However, since that’s a different process than your server, the database created and migrated ceases to exist when the migration process ends. Your server then starts by creating a new database.
I really don’t think you’re going to be able to get this to work with your default database.

Sorry, I’ll reply here only to make it easier. Yes I though it had to do with different process being run, that’s why I attempted to override the AppConfig ready method, and called the migrate command using call_command from django management, but still it apparently did nothing, because when I sent a request to the server, I still had the same issue.

I think I’ll save the trouble and go with a file db instead, seems easier and we don’t need an in-memory db for any complex reason, just an easy solution. Thanks anyway for the detailed response!