I have an allow_migrate method in my router.
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""Make sure the htmx_test app only appear in the 'htmx_test_db' database."""
if is_test_model(model_name):
log.debug(f"allow_migrate for {model_name}: {db} == htmx_test_db: {db == 'htmx_test_db'}")
return db == "htmx_test_db"
return None
def is_test_model(model: str | Model) -> bool:
"""Returns true if the name of the model starts with 'test' in any case (upper or lower)"""
if isinstance(model, str):
return model.lower().startswith("test")
return model.__name__.lower().startswith("test")
The ConnectionRouter finds my router correctly:
>>> from django.db.utils import ConnectionRouter
>>> router = ConnectionRouter()
>>> router.allow_migrate_model("htmx_test_db", testParent)
DEBUG allow_migrate for testparent: htmx_test_db == htmx_test_db: True
True```
When I make the migrations it finds the correct db:
root@bbf23f0dc481:/workspaces/food_safety# ./manage.py makemigrations
DEBUG allow_migrate for testParent: default == htmx_test_db: False
DEBUG allow_migrate for testChild: default == htmx_test_db: False
DEBUG allow_migrate for testParent: htmx_test_db == htmx_test_db: True
Migrations for 'htmx':
food_safety/apps/htmx/migrations/0001_initial.py
+ Create model testParent
+ Create model testChild```