0

Django 2.2.18

I want to split the models of one app to a second database. The relevant app is named "dear_zoho_analytics", which I assume is the 'app_label'. The original database is "default".

I have only one dbrouter, and it has code like this:

class DbRouter(object):
    
    route_app_labels = ["dear_zoho_analytics", ]
    def db_for_read(self, model, **hints):
        .
        .
        .


    def allow_migrate(self, db, app_label, model_name=None, **hints):
      
        if app_label in self.route_app_labels:
            return db == "dear_analytics"
        return None

when I do this:

python3 ./manage.py migrate --database=dear_analytics

I get the tables of every app created in the new database.

python3 ./manage.py migrate dear_zoho_analytics --database=dear_analytics does what I want. However, this sort of implies that my allow migrate is not working because it appears to do nothing useful.

2 Answers 2

1

What you want to do is to return False if the app_label is not "dear_zoho_analytics" when the db is "dear_analytics":

def allow_migrate(self, db, app_label, model_name=None, **hints):
    if app_label in self.route_app_labels:
        if db != "dear_analytics":
            return False
        return True
    elif db == "dear_analytics":
        return False
    return None

I have assumed you want this apps tables only in the "dear_analytics" db. Otherwise you may have to modify conditions a little.

Sign up to request clarification or add additional context in comments.

Comments

1

Your routing ensures that dear_zoho_analytics app is routed to dear_analytics, for rest of apps they are routed to all databases

simply adding additional condition would ensure other apps not being migrated to second database

def allow_migrate(self, db, app_label, model_name=None, **hints):
    if app_label in self.route_app_labels:
        return db ==  "dear_analytics"
    else:
        return db != "dear_analytics"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.