bernardopires / django-tenant-schemas

Tenant support for Django using PostgreSQL schemas.
https://django-tenant-schemas.readthedocs.org/en/latest/
MIT License
1.45k stars 425 forks source link

New tenant model is not creating relations in non-public schema with multiple databases #669

Open khixer opened 3 years ago

khixer commented 3 years ago

Hi,

Use Case:

I'm using this package for quite some time. We encountered a use case in which we have to introduce a new database. Adding a new schema won't suffice so we added another DB with multi-tenancy. Therefore, having two databases operating on a semi-isolated approach. Now, I'm facing an issue regarding the migration of a particular schema in the non-default database.

Code Two DB's are as follows:

"default": {
        "ENGINE": "tenant_schemas.postgresql_backend",
        "NAME": os.environ.get("DB_NAME", "****"),
        "USER": os.environ.get("DB_USER", "****"),
        "PASSWORD": "****",
    },
"non_default": {
        "ENGINE": "tenant_schemas.postgresql_backend",
        "NAME": os.environ.get("DB_NAME", "****"),
        "USER": os.environ.get("DB_USER", "****"),
        "PASSWORD": "***",
    }

Initially, I ran this command: python manage.py migrate_schemas --database=non_default. It creates necessary tables in the public schema

Now, when I'm trying to do: python manage.py migrate_schemas --database=non_default --schema=nds. Django is unable to find the schema nds. Also,

I created the tenant via ORM as suggested in the docs but it didn't create the tenant schema. Creating schema manually through create schema nds from psql didn't work either.

ORM code

tenant = Company(domain_url='xyz.nds.domain-url.com', schema_name='nds', name='nds')
tenant.save(using="non_default")

Didn't run any migration

Company.objects.using("non_default") shows the tenant model but no relations.

Any help would be highly appreciated.

Thanks!