Query.group_by() ignores arguments

Issue #2845 resolved
‮rekcäH nitraM‮ created an issue

Hi there,

I just spent an hour to debug why group_by was ignored for a query I was transforming, so I gather it's worth it to spend some more time to report a bug. We are using version 0.7.x so there may have been changes since, but I wasn't yet able to invest the time to create a testcase that reproduces this.

Here's what happens. Using the ORM I transform a query like this:

        query = statistics.track_items_for_topics_in_date_range_query([topic](topic), early_cutoff, late_cutoff)
        from sqlalchemy.sql import func
        query = query.with_entities(TrackItem.question_id, TrackItem.user_id, func.max(TrackItem.creation_time).label('max_creation_time'))
        query = query.group_by(TrackItem.question, TrackItem.user)

The problem/workaround is to not use the objects in the group_by() but instead the _id suffixes to tell sqlalchemy that it should group by the ids instead.

Now what happens if you don't know/see this is that the group_by() call is just ignored and the query is the exact same query after the call as it was before hand.

Which is, uhm, a bit hard to debug. :)

Comments (1)

  1. Log in to comment