﻿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
10626	Default model ordering affects annotation query	kmassey		"Example models without default orderings:
{{{
class Author(models.Model):
    id = models.IntegerField(primary_key=True,
         help_text='Uniquely identifies an author.')
    name= models.CharField(max_length=200,
         help_text=""Author's first name."")

class Book(models.Model):
    id = models.IntegerField(primary_key=True,
         help_text='Uniquely identifies a book.')
    title = models.CharField(max_length=200,
         help_text='The title of this book.')
    author = models.ForeignKey(Author, db_column='author_id',
         help_text='The author of this book.')
    price = models.FloatField(
         help_text='Price of the book.')
    inventory = models.IntegerField(
         help_text='Copies of book in the store.')
}}}

Use values() and annotate() to determine the number of books at each price. This works as I expected:
{{{
>>> q = Book.objects.values('price').annotate(Count('price'))
>>> list(q)
[{'price': 7.9900000000000002, 'price__count': 2}, {'price': 9.9900000000000002, 'price__count': 1}]
}}}

Now add default orderings to the models:
{{{
class Author(models.Model):
    id = models.IntegerField(primary_key=True,
         help_text='Uniquely identifies an author.')
    name= models.CharField(max_length=200,
         help_text=""Author's first name."")
    class Meta:
        ordering = ['id']

class Book(models.Model):
    id = models.IntegerField(primary_key=True,
         help_text='Uniquely identifies a book.')
    title = models.CharField(max_length=200,
         help_text='The title of this book.')
    author = models.ForeignKey(Author, db_column='author_id',
         help_text='The author of this book.')
    price = models.FloatField(
         help_text='Price of the book.')
    inventory = models.IntegerField(
         help_text='Copies of book in the store.')
    class Meta:
        ordering = ['id']
}}}

And repeat the query:

{{{
>>> q = Book.objects.values('price').annotate(Count('price'))
>>> list(q)
[{'price': 9.9900000000000002, 'price__count': 1}, {'price': 7.9900000000000002, 'price__count': 1}, {'price': 7.9900000000000002, 'price__count': 1}]
}}}

This isn't what I expected. It took me a while to make the connection between the model default ordering and the behavior of the annotation query. Once I saw it, the fix was easy:
{{{
>>> q = Book.objects.values('price').annotate(Count('price')).order_by()
>>> list(q)
[{'price': 7.9900000000000002, 'price__count': 2}, {'price': 9.9900000000000002, 'price__count': 1}]
}}}

I'm not sure if this should be a bug per se. If not, perhaps it could be noted in the aggregation documentation.

Thanks,
Kevin"		closed	Database layer (models, ORM)	1.1-beta		duplicate	values annotate ordering		Unreviewed	0	0	0	0	0	0
