Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OperationalError when attempting to filter by annotated field in m2m relation #1616

Open
aquarell-dev opened this issue May 16, 2024 · 1 comment

Comments

@aquarell-dev
Copy link

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.

@abondar
Copy link
Member

abondar commented May 17, 2024

Hello!

Please provide minimal reproducible code snippet, so that I could reproduce it locally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants