Tortoise-orm: After version 0.16.1 aggregation in aggregate classes works wrong

Created on 22 Mar 2020  路  4Comments  路  Source: tortoise/tortoise-orm

Describe the bug
A clear and concise description of what the bug is.
Post versions of 0.16.1 could cover _ _ aggregateing in annotate method but after 0.16.1 there are no more work as expect (ps. annotate is not root cause it is my example)

To Reproduce
Steps to reproduce the behavior, preferaby a small code snippet.

version < 0.16.1

tournaments = await Tournament.annotate(
    events_participants_count=Count("events__participants")
).filter(id=tournament.id)
print(tournaments.events_participants_count)

version >= 0.16.1

tournaments = await Tournament.annotate(
    events_participants_count=Count("events__participants")
).filter(id=tournament.id)
print(tournaments.events_participants_count)  # Error

exception like this:

  File "/storage/tortoise-orm/tortoise/queryset.py", line 604, in _resolve_annotate
    self._join_table_by_field(*join)
  File "/storage/tortoise-orm/tortoise/queryset.py", line 121, in _join_table_by_field
    self.query = self.query.join(join[0], how=JoinType.left_outer).on(join[1])
  File "/root/.pyenv/versions/3.7.6/envs/py37/lib/python3.7/site-packages/pypika/queries.py", line 1391, in on
    self.query.do_join(JoinOn(self.item, self.how, criterion, collate))
  File "/root/.pyenv/versions/3.7.6/envs/py37/lib/python3.7/site-packages/pypika/queries.py", line 969, in do_join
    join.validate(base_tables, self._joins)
  File "/root/.pyenv/versions/3.7.6/envs/py37/lib/python3.7/site-packages/pypika/queries.py", line 1485, in validate
    tables=", ".join(map(str, missing_tables))
pypika.utils.JoinException: Invalid join criterion. One field is required from the joined item and another from the selected table or an existing join.  Found ["event"]

Expected behavior
A clear and concise description of what you expected to happen.
version < 0.16.1

tournaments = await Tournament.annotate(
    events_participants_count=Count("events__participants")
).filter(id=tournament.id)
print(tournaments.events_participants_count)

Additional context
Add any other context about the problem here.
I think culprit commit is 42d85cb84f6684e7364446fc30ec930f0fd60ac4
(This is not for pressing commiter's responsibility but for more effective bug fix as information)

Lastly, I recommend you to add below test code

    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(len(tournaments), 1)
        self.assertEqual(tournaments[0].id, tournament.id)
regression

Most helpful comment

Thanks for bug reporting! I'll check the point

All 4 comments

Thanks for the detailed bugreport. Indeed that test case passes once we revert that commit.

Thanks for bug reporting! I'll check the point

I make hotfix PR to fix this bug. @pjongy Plz look that PR :)

Thank you for your quick and accurate work!

Was this page helpful?
0 / 5 - 0 ratings