tortoise / tortoise/tortoise-orm

Misleading error "Filtering by relation is not possible. Filter by nested field of related model"

Open
#435 3 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.