tortoise / tortoise/tortoise-orm
Misleading error "Filtering by relation is not possible. Filter by nested field of related model"
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
I had a ForeignKey m2 in a model M and wanted to M.filter(m2__id__isnull=None), but got the above error message. It took me some time to understand that I had to use .order_by(...), where ... does not contain m2, to get around the error. IMHO, the error should talk about fixing order_by().
Source line: https://github.com/tortoise/tortoise-orm/blob/8e3c294ef0cfcc577356b7864c79433cac1d2744/tortoise/queryset.py#L173
My version: tortoise-orm==0.16.13 (and using PostgreSQL)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at tortoise/queryset.py line 173 and reproduce the reported M.filter(m2__id__isnull=None) case with PostgreSQL, including an order_by() containing m2. Confirm the resulting error is caused by that ordering, then update the message so it explains the order_by fix; done when the message is no longer misleading.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, python
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100