简体   繁体   中英

Django Multiple Databases Connections

I have two databases and router defined in my settings.py

    'default': {
    },
    'primary': { 
         'ENGINE': 'django.db.backends.postgresql',
         'NAME': ...
    },
    'auth': {
         // similar to primary
    }

DATABASE_ROUTERS = ['app.database_router.AuthRouter','app.database_router.PrimaryReplicaRouter']

I am running into an issue where Django doesn't recognize the tables/relations within primary database . I tried reordering AuthRouter and PrimaryRouter but then Django won't recognize the User relations from auth database . Any suggestions? I tried making one of them the default but that doesn't solve the issue either.

This is my router with similar class for Auth:

class PrimaryReplicaRouter:
    def db_for_read(self, model, **hints):
        """
        Reads go to a read replica. We can add 'read_replica', 'write_replica'
        """
        return 'primary'

    def db_for_write(self, model, **hints):
        """
        Writes always go to primary.
        """
        return 'primary'

    def allow_relation(self, obj1, obj2, **hints):
        """
        Relations between objects are allowed if both objects are
        in the primary/replica pool.
        """
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        All non-auth models end up in this pool.
        """
        return True

class Auth:
     def db_for_read(self, model, **hints):
        return 'auth'

    def db_for_write(self, model, **hints):
        return 'auth'

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the auth app is involved.
        """
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        Make sure the auth app only appears in the 'auth'
        database.
        """
        return True

This is only speculation from my experience but I believe your problem lies within your Routers file. Changing the order causing a changed effect is completely normal because Django tries to resolve the request in the order using the routers in the order they are in the list. Ther error is probably similar in both routers as it seems that whichever one is first in the order is blocking the second from executing correctly.

Good luck hope this was remotely helpful!

One way to investigate something like this is to put some print statements in your code and see what the model._meta.app_label value is. After you figure those out, just proxy your requests to the correct database in your routers.py file:

class AuthRouter:
    def db_for_read(self, model, **hints):
        if model._meta.app_label in ['auth', 'django', 'sessions', 'admin']:
            return 'auth_db_name'
        return None

    def db_for_write(self, model, **hints):
        if model._meta.app_label in ['auth', 'django', 'sessions', 'admin']:
            return 'auth_db_name'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if app_label == 'auth_db_name':
            return False
        return True

class ApplicationRouter:
    def db_for_read(self, model, **hints):
        return 'app_db_name'

    def db_for_write(self, model, **hints):
        return 'app_db_name'

    def allow_relation(self, obj1, obj2, **hints):
        if obj1._state.db == 'app_db_name' and obj2._state.db == 'app_db_name':
            return True
        return False

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        return True

Make sure that your settings.py file has both DBs properly configured in via DATABASES configuration and that the DATABASE_ROUTER configuration is also set appropriately (note that it is

DATABASE_ROUTERS = ['routers.AuthRouter', 'routers.ApplicationRouter']

Reference: https://docs.djangoproject.com/en/1.8/topics/db/multi-db/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM