﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26925	Add a link to aggregation ordering interaction from Meta.ordering docs	Nicolas Joyard	nobody	"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"	Cleanup/optimization	closed	Documentation	1.8	Normal	fixed	aggregation		Accepted	1	0	0	0	0	0
