#Routers not routing migrations correctly

10 messages · Page 1 of 1 (latest)

mild vapor
#

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```

#

However, when I try to run the migrations, it never tries the htmx_test_db:

root@bbf23f0dc481:/workspaces/food_safety# ./manage.py migrate
DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testchild: default == htmx_test_db: False
DEBUG allow_migrate for testchild: default == htmx_test_db: False
DEBUG allow_migrate for testchild: default == htmx_test_db: False
DEBUG allow_migrate for testchild: default == htmx_test_db: False
DEBUG allow_migrate for testchild: default == htmx_test_db: False
Operations to perform:
  Apply all migrations: auth, contenttypes, core, htmx, sessions
Running migrations:
  Applying htmx.0001_initial...DEBUG allow_migrate for testparent: default == htmx_test_db: False
DEBUG allow_migrate for testchild: default == htmx_test_db: False
 OK```
#

And there is in fact no table created:

>>> print(testParent.objects.all())
Traceback (most recent call last):
  File "/usr/local/lib/python3.13/site-packages/django/db/backends/utils.py", line 105, in _execute
    return self.cursor.execute(sql, params)
           ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/django/db/backends/sqlite3/base.py", line 354, in execute
    return super().execute(query, params)
           ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
sqlite3.OperationalError: no such table: htmx_testparent```
Traceback continues, but basically has the same error multiple times.
Anyone have any suggestions?
mild vapor
#

I think I missed something in the migrations. I don't want to delete all this yet, but I'm looking at what I should have done re: ./manage.py migrate --database=htmx_test_db

vapid lily
#

As you pointed out, migrate needs to specify the database it's migrating

mild vapor
#

I haven't had a chance to get back to this yet. When I do I'll take another look at the migrations with specific databases.

mild vapor
#

I don't imagine it was keeping anyone awake at night, but I've got this worked out. I just gave it a while and tried again and it worked. 😛

vapid lily
#

Those might be my least satisfying answers 😁

humble oasis