citusdata / citusdata/django-multitenant

`prefetch_related()` does not filter on the tenant field

Open
#122 2 comments 0 reactions 0 assignees View on GitHub
missing_tests question
Dominant language
Python
Stars
823
Forks
126
PR merge metrics
No merged PRs in 30d

Description

The WHERE clause generated is just like`WHERE id IN (...)`

`prefetch_related` with the `Prefetch` object works:
```
prefetch_related(
Prefetch('field', Model.objects.all())
)
```

This workaround sometimes helps, but, for example, it does not work with @[django-auto-prefetch](https://github.com/tolomea/django-auto-prefetch)

P.S. `select_related` works as expected, it does filter by tenant.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the tenant-filtering behavior with the reported prefetch_related() call and compare its generated WHERE clause with Prefetch and select_related(), which the issue identifies as working. Check how django-auto-prefetch exercises this path, then consider the work done when prefetch queries consistently include the tenant constraint without breaking the reported working cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, postgresql, python
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.