Opened 14 years ago

Closed 9 years ago

Last modified 9 years 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: dev
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

Description

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 14 years ago.
14476-annotate-aggregate-r17389.diff (570 bytes ) - added by Tai Lee 13 years ago.

Download all attachments as: .zip

Change History (11)

by dirleyrls, 14 years ago

Attachment: patch added

comment:1 by dirleyrls, 14 years ago

Has patch: set

The patch applies to trunk r14233.

comment:2 by Russell Keith-Magee, 14 years ago

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 9 years ago by Tim Graham (previous) (diff)

comment:3 by Julien Phalip, 14 years ago

Severity: Normal
Type: Bug

comment:4 by Ramiro Morales, 13 years ago

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

In [16252]:

Fixed #14476 -- Fixed resolution of automatically generated annotation names so e.g. filtering based on them works. Thanks dirleyls for the report and patch.

comment:5 by Tai Lee, 13 years ago

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/query.py", line 331, in aggregate
    is_summary=True)
  File "/Users/mrmachine/django/db/models/sql/query.py", line 1000, in add_aggregate
    field_list, opts, self.get_initial_alias(), False)
  File "/Users/mrmachine/django/db/models/sql/query.py", 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 13 years ago by Tai Lee (previous) (diff)

comment:6 by fhahn, 12 years ago

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

comment:7 by Aymeric Augustin, 12 years ago

Status: reopenednew

comment:8 by Tim Graham, 9 years ago

Resolution: fixed
Status: newclosed

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

comment:9 by Tim Graham <timograham@…>, 9 years ago

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