tortoise / tortoise/tortoise-orm

OperationalError when attempting to filter by annotated field in m2m relation

Open
#1,616 1 comment 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

The problem
So, basically, i'm trying to filter a m2m related field, the m2m related field has profit and volume.
I created annotations for max_volume and max_profit, and also i wanna filter by the annotated field to exclude those rows where max_profit or max_volume is null, but i get the "column doesn't exist" error, which is clearly false.
Moreover, i've run the raw query on the database itself and it worked.

arbitrages = Arbitrage.all().annotate(
        max_profit=Max(
            "offers__profit",
            _filter=Q(offers__profit__gt=profit)
        )
    ).annotate(
        max_volume=Max(
            "offers__volume",
            _filter=Q(offers__volume__lt=volume + 1)
        )
    ).filter(max_volume__isnull=False).prefetch_related(
        "buy",
        "sell",
        "network",
        Prefetch("offers", queryset=Offer.filter(Q(volume__lt=volume + 1) & Q(profit__gt=profit)))
    ).order_by("-max_volume", "-max_profit")

this method .filter(max_volume__isnull=False) basically triggers the error

Expected behavior
I'm a bit confused here, it should just perform the filtering, but somehow it just won't.
Moreover, due to a lack of exeperience with Tortoise i can't even write my own query and convert it to QuerySet, well, i can, but it ain't gonna look pretty.

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 from the queryset path combining annotate(), filter(), Max, Q, and the many-to-many offers relation. Reproduce the failing query and inspect the generated SQL around filtering on the annotated max_volume field, comparing it with the raw query that works. Done means filtering by the annotation no longer produces a missing-column error and the expected rows are returned.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.