﻿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
26434	Inconsistent results of QuerySet.count() when ordering is not a subset of explicit grouping.	kamandol	Michal Mládek	"Using a PostgreSQL backend database, in some cases with QuerySets involving '''annotations''', '''values''' or '''values_list''' and ordering('''order_by''') clauses the resulting QuerySet '''count()''' method fails to predict the real row result unless that QuerySet is evaluated.

For instance:

{{{
#!python
q = BundleVersion.objects.order_by('-id').values_list(""port__id"", ""asset_bundle__id"").annotate(max=models.Max(""id""))
}}}

This QuerySet using PostgreSQL would, in fact, group the results by '''BundleVersion.id''' instead of the tuple (""port!__id"", ""asset_bundle!__id"") due to the '''order_by''' clause using a column not in the '''values_list'''. This is a documented behavior.

The problem is that if '''q''' is not evaluated yet, calling '''q.count()''' will return an amount of rows as if the grouping was done on the tuple (""port!__id"", ""asset_bundle!__id""). '''After''' evaluating '''q''', or calling '''len(q)''', the result of '''q.count()''' will be the expected, as if the grouping was done using ''''BundleVersion.id''''.

"	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed	postgresql queryset count annotate aggreagate order_by	github@… Simon Charette	Ready for checkin	1	0	0	0	0	0
