bernardopires / django-tenant-schemas

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

Delete object from public with foreign key table in tenant: Relation not found #504

Open durkode opened 7 years ago

durkode commented 7 years ago

I have a setup where my users table is in both public and tenants, whilst my entities table is just in tenants. There is a many to many relationship set on the Entity model to the User model.

I can not delete a user from the public schema. I get the following error:

ProgrammingError: relation "entities_entity_clients" does not exist LINE 1: DELETE FROM "entities_entity_clients" WHERE "entities_entity...

Where a client is a User model. This makes sense, as django doesn't have the concept of tenants and just sees the many to many field attached so tried to delete from it. What is the best approach to remedying this? Is there a way to disregard this error in django?

The only solution I can think of is to essentially create a special "dummy" tenant schema and append it to the search path when running these queries. That should work, however it feels like a poor fix so I was wondering if anyone who has run into this has a better idea.

jonperron commented 7 years ago

My 2 cents on your question @durkode would be to keep as much as possible the FK relations within the same tenant and avoids cross-tenancy possible relations. I got a similar problem and I did only a "dummy" tenant models handling the routing and the setting of the schema prior to the DB requests.

sandrabg86 commented 4 years ago

I have the same problem, some news about that?

arasic commented 2 years ago

Not sure if this could help. What you can do is use a similar method with schema_context, but instead of having only the specific schema (not the shared one, but the specific schema), and append the 'public' schema in the arguments as follows:

with schema_context(schema_name, 'public'):

That will set in the search_path:

SET search_path = 'schema_name_value','public`