#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 )
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)
Change History (6)
comment:1 by , 16 years ago
Description: | modified (diff) |
---|---|
Summary: | QuerySet values() should work on extra(select) fields → Aggregations add extra values to ValuesQuerySets |
by , 16 years ago
Attachment: | 10132-test.diff added |
---|
comment:2 by , 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 , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:5 by , 12 years ago
Component: | ORM aggregation → Database layer (models, ORM) |
---|
Note:
See TracTickets
for help on using tickets.
Fixed description formatting.
Also changed the title to reflect the problem. The
extra()
andvalues()
methods already work together. It's only the final call to add aggregation in that is triggering this.