bernardopires / django-tenant-schemas

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

How is handled FK relationship from tenant in public schema to models in tenants schemas #499

Closed jonperron closed 7 years ago

jonperron commented 7 years ago

Hello,

I have a question which is related to #420, but in the other way !

Indeed, I was wondering how django-tenant-schemas handle FK relationship from the tenant model in the public schema towards models in the tenants schemas. For example, is it possible to set the Tenant schema as:

class Foo(TenantMixin):
    bar = models.Foreignkey(Bar, null=True, blank=True, on_delete=models.SET_NULL)

And in a tenant barapp:

class Bar(models.Models):
    pass

When I do so and following make_migrations, python manage.py migrate_schemas --shared is raising a psycopg2.ProgrammingError: relation "bar_bar" does not exist, which I think is pretty logical as the bar model will only exist in the tenant schemas.

harikvpy commented 7 years ago

The question of TenantModel having a FK relationship to model in the schema doesn't arise as there's no need for such a design. Perhaps you can explain what real-life problem motivated you to ask this?

jonperron commented 7 years ago

Hello,

Thank you for your answer !

A specific example of this behaviour is displayed by Microsoft and Office 365, where you log on a common page and are redirected towards your organization Office 365.

Brainstorming about this issue lead us to observe that our issue is beyond the scopes of your app; it is more related to the authentication and how the Django ORM is maintaining FK relationship. In simple words, the FK is not the id of a model in our case, but the schema.id of the model.