Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#26925 closed Cleanup/optimization (fixed)

Add a link to aggregation ordering interaction from Meta.ordering docs

Reported by: Nicolas Joyard Owned by: nobody
Component: Documentation Version: 1.8
Severity: Normal Keywords: aggregation
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Consider the following model definition:

class Thing(Model):
    foo = SomeField()
    bar = SomeField()
    
    class Meta:
        ordering = ('foo',)

Imagine I would like to count instances of Things for each 'bar' value:

qs = Thing.objects.all.values('bar').annotate(cnt=Count('bar'))

I would expect the ORM to execute something along the lines of

SELECT bar, COUNT(bar) FROM things GROUP BY bar

Except the ORM wants to enforce the default ordering, and the only way to do so is to execute

SELECT bar, COUNT(bar) FROM things GROUP BY foo, bar ORDER BY foo

...which is a completely different query. As a user of the model, I have to add an ordering override (that I didn't need and that adds an ordering step in the query execution plan):

qs = Thing.objects.all.values('bar').annotate(cnt=Count('bar')).order_by('bar')

I would expect the ORM to either:

  • ignore the default ordering, given that it uses fields that are no longer retrieved in the query
  • fail executing the query with an exception stating that my aggregation is not compatible with the model default ordering

Change History (6)

comment:1 by Simon Charette, 8 years ago

I'm tempted to close this as wontfix since this is a documented caveat of aggregating over a model with a default ordering.

Maybe we could add another warning to the Meta.ordering docs pointing to aforementioned caveat as silently ignoring the ordering is not an option at this point as it would be backward incompatible.

comment:2 by Nicolas Joyard, 8 years ago

Oh, sorry, I completely missed that paragraph of documentation.

comment:3 by Tim Graham, 8 years ago

Component: Database layer (models, ORM)Documentation
Has patch: set
Summary: ORM changes grouping when default ordering is setAdd a link to aggregation ordering interaction from Meta.ordering docs
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

PR to add a doc link.

comment:4 by GitHub <noreply@…>, 8 years ago

Resolution: fixed
Status: newclosed

In 3aaf6cf:

Fixed #26925 -- Linked aggregation ordering topic from Meta.ordering docs.

comment:5 by Tim Graham <timograham@…>, 8 years ago

In bdeec1d:

[1.9.x] Fixed #26925 -- Linked aggregation ordering topic from Meta.ordering docs.

Backport of 3aaf6cf0f3288986c4ce56defea26420f8a48534 from master

comment:6 by Tim Graham <timograham@…>, 8 years ago

In a4df657:

[1.10.x] Fixed #26925 -- Linked aggregation ordering topic from Meta.ordering docs.

Backport of 3aaf6cf0f3288986c4ce56defea26420f8a48534 from master

Note: See TracTickets for help on using tickets.
Back to Top