Opened 8 years ago

Closed 8 years ago

Last modified 4 years ago

#10132 closed (fixed)

Aggregations add extra values to ValuesQuerySets

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

Description (last modified by Malcolm Tredinnick)


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


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 8 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 8 years ago by Malcolm Tredinnick

Description: modified (diff)
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
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.

Changed 8 years ago by Glenn Maynard

Attachment: 10132-test.diff added

comment:2 Changed 8 years ago by Glenn Maynard

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 Changed 8 years ago by Russell Keith-Magee

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 Changed 8 years ago by Russell Keith-Magee

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

comment:5 Changed 4 years ago by Anssi Kääriäinen

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