4👍
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.”
15👍
For anyone facing the issue I was having, here is what I finally settled on:
routers.py:
from django.conf import settings
class DatabaseAppsRouter(object):
def db_for_read(self, model, **hints):
# your db routing
def db_for_write(self, model, **hints):
# your db routing
def allow_relation(self, obj1, obj2, **hints):
# your db routing
def allow_syncdb(self, db, model):
# your db routing
def allow_migrate(self, db, app_label, model_name=None, **hints):
if db in settings.DATABASE_APPS_MAPPING.values():
return settings.DATABASE_APPS_MAPPING.get(app_label) == db
elif app_label in settings.DATABASE_APPS_MAPPING:
return False
settings.py
DATABASE_ROUTERS = ['project.routers.DatabaseAppsRouter']
DATABASE_APPS_MAPPING = {'contenttypes': 'default',
'auth': 'default',
'admin': 'default',
'sessions': 'default',
'messages': 'default',
'staticfiles': 'default',
'myapp': 'mydb',
}
this setup lets me to run two commands:
python manage.py migrate
python manage.py migrate --database=mydb
which is still one to many but at least now I cannot mistakenly migrate myapp to the default database which happened more times that I want to admit.
Warning – rant:
Up to this point I was finding the Django design to be quircky but very logical and programmer friendly, like python itself. But the way that migrations interact with multiple database setups is really inconvenient makes it so easy to flood your default database with unwanted tables.
Discussion linked by kevin in his answer shows that I am not the only one to have this kind of difficulties.
- Is a varchar 2 more efficient than a varchar 255?
- How to redirect users to a specific url after registration in django registration?
- Django's annotate Count with division returns integer instead of float
- Django Query distinct values works but i cant use the query result
- Cannot load library libcairo