You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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))
The text was updated successfully, but these errors were encountered:
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:
Query:
Expected behavior
Expected result is Player instances with ages more than 21
Additional context
Full traceback:
After that error I used manual SQL that actually gave me correct result
The text was updated successfully, but these errors were encountered: