Opened 16 years ago

Closed 16 years ago

Last modified 12 years ago

#10132 closed (fixed)

Aggregations add extra values to ValuesQuerySets

Reported by: Glenn Maynard Owned by:
Component: Database layer (models, ORM) Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Malcolm Tredinnick)

From http://groups.google.com/group/django-users/browse_frm/thread/15b3c24dddd2a2d5/2214eba4328126ca

Item.objects.extra(select={"note_alias": "note"}).values("note_alias").annotate(Count("id")).order_by('note_id')

generates:

SELECT (note_id) AS "note_alias", U0."id", 
COUNT("queries_item"."id") AS "id__count" FROM "queries_item" U0 
GROUP BY "queries_item"."id", "queries_item"."name", 
"queries_item"."created", "queries_item"."modified", 
"queries_item"."creator_id", "queries_item"."note_id"

but should (possibly) generate:

SELECT (note_id) AS "note_alias",
COUNT("queries_item"."id") AS "id__count" FROM queries_item
 GROUP BY note_alias ORDER BY note_id

Attachments (1)

10132-test.diff (832 bytes ) - added by Glenn Maynard 16 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 by Malcolm Tredinnick, 16 years ago

Description: modified (diff)
Summary: QuerySet values() should work on extra(select) fieldsAggregations add extra values to ValuesQuerySets

Fixed description formatting.

Also changed the title to reflect the problem. The extra() and values() methods already work together. It's only the final call to add aggregation in that is triggering this.

by Glenn Maynard, 16 years ago

Attachment: 10132-test.diff added

comment:2 by Glenn Maynard, 16 years ago

Manually adding to query.group_by works around this, at least for this case (also fixed up the test, since I couldn't test the passing case before):

>>> from django.db.models import Count
>>> test = Item.objects.extra(select={"note_alias": "note_id"}).values("note_alias")
>>> test.query.group_by.append("note_alias")
>>> test.order_by("note_alias").annotate(Count("id"))
[{'note_alias': 1, 'id__count': 2}, {'note_alias': 2, 'id__count': 1}, {'note_alias': 3, 'id__count': 3}]

comment:3 by Russell Keith-Magee, 16 years ago

Resolution: fixed
Status: newclosed

(In [9838]) Fixed #10132 -- Corrected the interaction of extra() queries with the values() clause. Thanks to Glen Maynard for the report.

comment:4 by Russell Keith-Magee, 16 years ago

Apologies for misspelling your name in the commit message, Glenn.

comment:5 by Anssi Kääriäinen, 12 years ago

Component: ORM aggregationDatabase layer (models, ORM)
Note: See TracTickets for help on using tickets.
Back to Top