diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4f6b7bcc2..693cd82f5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -14,6 +14,7 @@ Changelog * Default ``values()`` & ``values_list()`` now includes annotations. * Annotations over joins now work correctly with ``values()`` & ``values_list()`` * Ensure ``GROUP BY`` precedes ``HAVING`` to ensure that filtering by aggregates work correctly. +* Fix bug with join query with aggregation 0.16.1 ------ diff --git a/tests/test_aggregation.py b/tests/test_aggregation.py index a626db6d4..3976e9c2e 100644 --- a/tests/test_aggregation.py +++ b/tests/test_aggregation.py @@ -50,6 +50,22 @@ async def test_aggregation(self): with self.assertRaisesRegex(ConfigurationError, "name__id not resolvable"): await Event.all().annotate(tournament_test_id=Sum("name__id")).first() + async def test_nested_aggregation_in_annotation(self): + tournament = await Tournament.create(name="0") + await Tournament.create(name="1") + event = await Event.create(name="2", tournament=tournament) + + team_first = await Team.create(name="First") + team_second = await Team.create(name="Second") + + await event.participants.add(team_second) + await event.participants.add(team_first) + + tournaments = await Tournament.annotate( + events_participants_count=Count("events__participants") + ).filter(id=tournament.id) + self.assertEqual(tournaments[0].events_participants_count, 2) + async def test_aggregation_with_distinct(self): tournament = await Tournament.create(name="New Tournament") await Event.create(name="Event 1", tournament=tournament) diff --git a/tortoise/functions.py b/tortoise/functions.py index 224b62534..2d88a2ee1 100644 --- a/tortoise/functions.py +++ b/tortoise/functions.py @@ -106,7 +106,6 @@ def resolve(self, model: "Type[Model]", table: Table) -> dict: if isinstance(self.field, str): function = self._resolve_field_for_model(model, table, self.field, *self.default_values) - function["joins"] = reversed(function["joins"]) return function else: field, field_object = F.resolver_arithmetic_expression(model, self.field)