I just cannot get my database routers to work correctly. I have read, and read, and researched, and tested, and tested, and still nothing.
Here is my router:
class TenantBaseRouter(object):
def db_for_read(self, model, **hints):
return 'tenant_base'
def db_for_write(self, model, **hints):
return 'tenant_base'
def allow_relation(self, obj1, obj2, **hints):
return True
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == 'test_app':
if db == 'tenant_base' and model_name in tenant_models:
return True
return False
Yes, I have read the latest documentation about database routers, as printed here: Multiple databases | Django documentation | Django
Regarding allow_migrate
, the documentation states, " Return True
if the operation should run, False
if it shouldn’t run, or None
if the router has no opinion."
However, scroll down the page a bit, down to the examples, and this is the code example:
def allow_migrate(self, db, app_label, model_name=None, **hints):
"""
Make sure the auth and contenttypes apps only appear in the
'auth_db' database.
"""
if app_label in self.route_app_labels:
return db == "auth_db"
return None
So, one part of the documentation indicates return True, False, or None. Another part of the documentation indicates return db == "auth_db"
(which actually doesn’t even make sense).
I am specifying the exact database when I run the migrations:
python manage.py migrate --database=tenant_base
When I run the migrations, as specified, NONE of the models in question migrate to the table in question. Instead, all of Django’s built in models migrate over, despite not matching the conditions specified in allow_migrate
.
I am at a total loss.