Django Multi-tenant postgresql and mongoDB

149 views Asked by At

I implemented, PostgreSQL and MongoDB in a project. MongoDB was used to saved logs. It was working fine before adding multi-tenancy in the postgresql using django-tenants=3.5.0

I created another branch and removed the MongoDB from it. So just the Postgresql with multi-tenants. It was also working fine.

But the issue arises when I merge the branch and integrate the both Databases (PostgreSQL + MongoDB). I got the following error on migrate.

AttributeError: 'DatabaseWrapper' object has no attribute 'set_schema'

On exploring, I found this issue is related to database engine, so it should be django_tenants.postgresql_backend for PostgreSQL in-case of Django-tenant. But I'm already using it. and for MongoDB the engine is djongo .

I'm also using database routes

DATABASE_ROUTERS = ['logs.routers.MongoRouter', 'django_tenants.routers.TenantSyncRouter', ]

here's the custom mongodb router.py

class MongoRouter:
    """
    A router to control if database should use
    primary database or non-relational one.
    """

    mongo_models = {'logs'}

    def db_for_read(self, model, **_hints):
        if model._meta.model_name in self.mongo_models:
            return 'mongodb'
        return 'default'

    def db_for_write(self, model, **_hints):
        if model._meta.model_name in self.mongo_models:
            return 'mongodb'
        return 'default'

    def allow_migrate(self, _db, _app_label, model_name=None, **_hints):
        if _db == 'mongodb' or model_name in self.mongo_models:
            return False
        return True

Any solution, to this situation would be really appreciated. Let me know if further details are required.

0

There are 0 answers