Running migrations with multiple databases

37 views
Skip to first unread message

Shaheed Haque

unread,
Jul 10, 2023, 8:26:34 AM7/10/23
to django...@googlegroups.com
Hi,

I'm on Django 4.2 atop Postgres. In my project settings.py, I have a main/default database connection, and a second set up like this:

==============
DATABASES = {
    'default': {
        ...
        'NAME': 'foo',                     # Postgres DATABASE NAME
        ...
    },
    'archive_restore': {
        ...
        'NAME': 'archive_restore',   # Postgres DATABASE NAME
        ...
    },
    # Other entries for specialised purposes such as custom Postgres Foreign Data Wrappers.
===============

As you can see, the Postgres database names are "foo" and "archive_restore" respectively. For all normal Django purposes, we want to use "default", aka "foo". The "archive_restore" connection/database is used in conjunction with a bunch of psql commands to create a subset of the main Django ORM data. Once populated via pg_restore, I need to run the DJango migrations on them for eventual use under Django. I had assumed that a command like this:

./manage.py migrate --database archive_restore

would modify archive_restore/archive_restore. However, what seems to happen is that the migrations are:
  • Actually run in default/foo (the migrations include code generated by makemigrations and custom RunPython stuff).
  • But recorded in the django_migrations table in archive_restore/archive_restore.
whereas I was expecting that they would be both run and recorded in the latter. Have I overlooked some setting/restriction, or is this a bug?

Thanks, Shaheed




David Nugent

unread,
Jul 10, 2023, 9:48:48 PM7/10/23
to django...@googlegroups.com
Migrations are executed according to how your db routers is setup.

See setting DATABASE_ROUTERS.

Note that normally you would be replicating secondaries, so migrations are normally not applied on all bu the primary, so this is the default.

Regards,
David

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHAc2je%2B%3D67tZBeGBMUO8Dmqmq9SaCRuopLxqrECuXb1C8YriQ%40mail.gmail.com.

shahee...@gmail.com

unread,
Jul 11, 2023, 4:20:55 AM7/11/23
to Django users
Thanks: just what I needed to know!
Reply all
Reply to author
Forward
0 new messages