Raff89 Raff89 - 1 month ago 5x
Python Question

Automatic db router for migrations in django

I am building a Django powered site and I want to have separate databases for some of the apps, I build a flexible router that routes each app to predefined database, this works fine.
The problem is that when I am migrating my models I have to set the

parameter every time and I find this annoying and redundant. Also many times I flooded my default database with tables from migrated app (by forgetting to add
I experimented with
function in my router but all I could achieve is safety mechanism that will not run the migration if I forget to specify the database.
My question is: is there a way to set up a automatic database selection for models migrations in Django?
My approach might be wrong, it surprises me that no one seems to have done that before.


I don't know of any way to do that automatically. However, a simple approach might be to write your own version of the migrate command that calls the Django migrate command multiple times with the appropriate --database and app arguments.

According to the author of Django migrations, this is a conscious design decision, so I wouldn't expect it to change: "Just like syncdb, migrate only runs on one database at a time, so you must execute it individually for each database, as you suggest. This is by design."