citusdata / django-multitenant

Python/Django support for distributed multi-tenant databases like Postgres+Citus
MIT License
712 stars 117 forks source link

2.0.6 fails to add the tenant in create() and subqueries #47

Closed KrYpTeD974 closed 5 years ago

KrYpTeD974 commented 5 years ago

It seems that since version 2.0.6, the TenantQuerySetMixin has been removed. Now the tenant_id is not set when performing a create(). The subqueries are also not filtered when joining other tables.

See this pull request More particularly, this file :

louiseGrandjonc commented 5 years ago

Hi @KrYpTeD974,

thanks for reporting this, I am fixing this. The reason why we removed TenantQuerySetMixin is that users can now easily use their own TenantQueryset using this

class MyManager(TenantManagerMixin, models.Manager):
    _queryset_class = MyQueryset

When I was working on this I couldn't reproduce cases where subqueries needed this block https://github.com/citusdata/django-multitenant/pull/45/commits/0f3d67c9a07b5ee4f1ba0296a5d2fc8e59f9f36b#diff-6affa3189b6033e7615a6bc23fb18f8eL22

Could you send me your usecase?

louiseGrandjonc commented 5 years ago

Here is the subquery test https://github.com/citusdata/django-multitenant/blob/multi-tenant/django_multitenant/tests/test_models.py#L212

louiseGrandjonc commented 5 years ago

I added a second test with what you said, with joins in subqueries: here is the query generated

SELECT "tests_project"."id", "tests_project"."name", "tests_project"."account_id", 
(SELECT U0."name" 
FROM "tests_subtask" U0 
INNER JOIN "tests_task" U3 ON (U0."task_id" = U3."id" AND (U0."account_id" = (U3."account_id"))) 
WHERE (U0."account_id" = 1 AND U0."project_id" = ("tests_project"."id") AND U3."opened" = true)
ORDER BY U0."name" DESC LIMIT 1) AS "first_subtask_name" 
FROM "tests_project" WHERE "tests_project"."account_id" = 1

The fact that there is a filter UO."account_id" = 1 and the join in the subquery has (U0."account_id" = (U3."account_id") is enough to filter the subquery on the current tenant