Skip to content

Commit

Permalink
Annotated count fix index error (#1707)
Browse files Browse the repository at this point in the history
* Fix for annotated count

* updated changelog

* added tests to show what the fix is about
  • Loading branch information
Chr0nos authored Oct 10, 2024
1 parent 4a8b3d2 commit 25d0624
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Changelog

0.21
====
0.21.7
------
- Fix bug when using annotate and count at the same time but the annotation does not match anything, leading to an IndexError

0.21.6
------
Expand Down
7 changes: 7 additions & 0 deletions tests/test_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,10 @@ async def test_count_after_aggregate_m2m(self):

res = await query.count()
assert res == 2

async def test_count_without_matching(self) -> None:
await Tournament.create(name="Test")

query = Tournament.annotate(events_count=Count("events")).filter(events_count__gt=0).count()
result = await query
assert result == 0
2 changes: 2 additions & 0 deletions tortoise/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,8 @@ def __await__(self) -> Generator[Any, None, int]:

async def _execute(self) -> int:
_, result = await self._db.execute_query(str(self.query))
if not result:
return 0
count = list(dict(result[0]).values())[0] - self.offset
if self.limit and count > self.limit:
return self.limit
Expand Down

0 comments on commit 25d0624

Please sign in to comment.