Opened 6 years ago

Closed 14 months ago

Last modified 14 months ago

#14476 closed Bug (fixed)

annotate, default aggregation naming and filter annoyance

Reported by: dirleyrls Owned by: Ramiro Morales
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: annotate, FieldError
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no


Going straight to the point, if I do the following:

from django.db import models
class Answer(models.Model):
    entry = models.ForeignKey(Question)
qsa = Question.objects.annotate(answer_count=models.Count('answer')).filter(answer_count=0)
qsb = Question.objects.annotate(models.Count('answer')).filter(answer__count=0)

qsa will be correctly evaluted, but qsb will raise a "FieldError: Cannot resolve keyword 'count' into field. Choices are ... answer_ _count".

Now, isn't this annoying? The error message is lying!

Attachments (2)

patch (580 bytes) - added by dirleyrls 6 years ago.
14476-annotate-aggregate-r17389.diff (570 bytes) - added by Tai Lee 5 years ago.

Download all attachments as: .zip

Change History (11)

Changed 6 years ago by dirleyrls

Attachment: patch added

comment:1 Changed 6 years ago by dirleyrls

Has patch: set
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

The patch applies to trunk r14233.

comment:2 Changed 6 years ago by Russell Keith-Magee

Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

There's a slight problem here in that filter(answer__count) could be referring to a default aggregate alias, or a field called count on the answer model. However, I think we can live with that ambiguity.

Patch requires tests.

Last edited 14 months ago by Tim Graham (previous) (diff)

comment:3 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:4 Changed 5 years ago by Ramiro Morales

Owner: set to Ramiro Morales
Resolution: fixed
Status: newclosed

In [16252]:

(The changeset message doesn't reference this ticket)

comment:5 Changed 5 years ago by Tai Lee

Easy pickings: unset
Resolution: fixed
Status: closedreopened
UI/UX: unset

This is still a problem for aggregates.

>>> from django.contrib.auth.models import *
>>> from django.db.models import *
>>> User.objects.annotate(xyz=Count('groups')).aggregate(Max('xyz'))
{'xyz__max': 13}
>>> User.objects.annotate(Count('groups')).aggregate(Max('groups__count'))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/mrmachine/django/db/models/", line 331, in aggregate
  File "/Users/mrmachine/django/db/models/sql/", line 1000, in add_aggregate
    field_list, opts, self.get_initial_alias(), False)
  File "/Users/mrmachine/django/db/models/sql/", line 1288, in setup_joins
    "Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'count' into field. Choices are: id, name, permissions, user, groups__count

exclude(), filter(), values(), values_list() and order_by() all work, so I assume that aggregate() should as well.

Can we apply the same fix? Let me know if I should open a new ticket instead of re-opening this one.

Patch with updated test attached.

Last edited 5 years ago by Tai Lee (previous) (diff)

Changed 5 years ago by Tai Lee

comment:6 Changed 4 years ago by fhahn

Component: ORM aggregationDatabase layer (models, ORM)
Version: 1.2master

comment:7 Changed 4 years ago by Aymeric Augustin

Status: reopenednew

comment:8 Changed 14 months ago by Tim Graham

Resolution: fixed
Status: newclosed

That test case is fixed by f59fd15c4928caf3dfcbd50f6ab47be409a43b01 (Django 1.8). I will commit the test.

comment:9 Changed 14 months ago by Tim Graham <timograham@…>

In 3e1bb5c:

Refs #14476 -- Added a test for default annotation name access in aggregate.

Fixed in f59fd15c4928caf3dfcbd50f6ab47be409a43b01

Note: See TracTickets for help on using tickets.
Back to Top