Changes between Initial Version and Version 1 of Ticket #26434, comment 15


Ignore:
Timestamp:
May 16, 2025, 3:37:22 AM (4 months ago)
Author:
Simon Charette

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #26434, comment 15

    initial v1  
    11FWIW the issue is not Postgres specific (I reproduced against SQLite).
    22
    3 The problem appears to be caused by `sql.Query.get_aggregation`'s call to `clear_ordering(force=False)`. It seems like the later should skip clearing when `isinstance(self.group_by, tuple)`  and `self.order_by` is not a subset of `self.group_by` as `force=False` must only clear if doing to preserves the semantic of the query which it doesn't if `order_by` includes members missing from the group by (as they would normally be added).
     3The problem appears to be caused by `sql.Query.get_aggregation`'s [https://github.com/django/django/blob/994dc6d8a1bae717baa236b65e11cf91ce181c53/django/db/models/sql/query.py#L528-L531 call] to `clear_ordering(force=False)`. It seems like [https://github.com/django/django/blob/994dc6d8a1bae717baa236b65e11cf91ce181c53/django/db/models/sql/query.py#L2313-L2316 the later should skip clearing] when `isinstance(self.group_by, tuple)`  and `self.order_by + self.extra_order_by` is not a subset of `self.group_by` as `force=False` must only clear if doing so preserves the semantic of the query which clearly doesn't if the ordering includes members missing from the group by ([https://github.com/django/django/blob/994dc6d8a1bae717baa236b65e11cf91ce181c53/django/db/models/sql/compiler.py#L164-L169 as they would normally be added at a later stage]).
    44
    55I adjusted the ticket summary accordingly.
Back to Top