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

Cannot use annotated expression in filter #806

Closed
Masynchin opened this issue Jun 27, 2021 · 1 comment · Fixed by #807
Closed

Cannot use annotated expression in filter #806

Masynchin opened this issue Jun 27, 2021 · 1 comment · Fixed by #807

Comments

@Masynchin
Copy link

Describe the bug
I have AttributeError: 'ArithmeticExpression' object has no attribute 'resolve' error when trying to filter models by F expression. I want to filter players who are older than 21 years.

To Reproduce
Model:

class Player(Model):
    id = fields.IntField(pk=True)
    nation = fields.CharField(max_length=32)
    firstname = fields.CharField(max_length=32)
    lastname = fields.CharField(max_length=32)
    fullname = fields.CharField(max_length=64)
    position = fields.CharField(max_length=3)
    skill = fields.SmallIntField()
    birthday = fields.DateField()
    market_value = fields.IntField()

    club = fields.ForeignKeyField(
        "models.Club", related_name="squad", null=True
    )

Query:

import datetime as dt
from tortoise.expressions import F

players = await (
    Player
    .annotate(age = dt.datetime.now() - F("birthday"))
    .filter(age__gt=21)
)

Expected behavior
Expected result is Player instances with ages more than 21

Additional context
Full traceback:

Traceback (most recent call last):
  File "C:\fotbolti\tests\test_search_service\test_search_players.py", line 130, in test_something
    [players], *_ = await search_service.search_players()
  File "C:\fotbolti\fotbolti\search_service.py", line 38, in search_players
    players = await (
  File "c:\users\...\appdata\local\pypoetry\cache\virtualenvs\fotbolti-0qb8cmjl-py3.8\lib\site-packages\tortoise\queryset.py", line 871, in __await__
    self._make_query()
  File "c:\users\...\appdata\local\pypoetry\cache\virtualenvs\fotbolti-0qb8cmjl-py3.8\lib\site-packages\tortoise\queryset.py", line 836, in _make_query
    self.resolve_filters(
  File "c:\users\...\appdata\local\pypoetry\cache\virtualenvs\fotbolti-0qb8cmjl-py3.8\lib\site-packages\tortoise\queryset.py", line 126, in resolve_filters
    modifier &= node.resolve(model, annotations, custom_filters, model._meta.basetable)
  File "c:\users\...\appdata\local\pypoetry\cache\virtualenvs\fotbolti-0qb8cmjl-py3.8\lib\site-packages\tortoise\query_utils.py", line 386, in resolve
    return self._resolve_kwargs(model, table)
  File "c:\users\...\appdata\local\pypoetry\cache\virtualenvs\fotbolti-0qb8cmjl-py3.8\lib\site-packages\tortoise\query_utils.py", line 342, in _resolve_kwargs
    filter_modifier = self._resolve_custom_kwarg(model, key, value, table)
  File "c:\users\...\appdata\local\pypoetry\cache\virtualenvs\fotbolti-0qb8cmjl-py3.8\lib\site-packages\tortoise\query_utils.py", line 284, in _resolve_custom_kwarg
    annotation_info = annotation.resolve(model, table)
AttributeError: 'ArithmeticExpression' object has no attribute 'resolve'

After that error I used manual SQL that actually gave me correct result

from tortoise.transactions import in_transaction

async with in_transaction("default") as tconn:
    res, [row] = await tconn.execute_query("""
        SELECT "nation","fullname","firstname","birthday","position","skill","market_value","club_id","lastname","id",'2021-06-27T04:30:49.584324'-"birthday"
"age"
        FROM "player"
        WHERE '2021-06-27T04:30:49.584324'-"birthday" > 21
    """)
    print(list(row))
@long2ice
Copy link
Member

Need to be fixed

long2ice added a commit that referenced this issue Jun 27, 2021
long2ice added a commit that referenced this issue Jun 27, 2021
* Fix `filter` error after `annotate` with `F`. (#806)

* Fix test
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

Successfully merging a pull request may close this issue.

2 participants