Opened 2 years ago
Closed 2 years ago
#34024 closed Bug (fixed)
'WhereNode' object has no attribute 'is_summary' when counting a queryset with annotation from a subquery
Reported by: | Valentin Rigal | Owned by: | David Sanders |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 4.1 |
Severity: | Normal | Keywords: | |
Cc: | David Sanders, David Wobrock | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I reproduced on a blank project (4.0 and 4.1 with a Postgres database) using this use case:
models.py
class School(models.Model): """A school""" class Director(models.Model): school = models.ForeignKey(School, on_delete=models.CASCADE) speciality = models.CharField(max_length=200)
It is not possible to run this code:
>>> School.objects.annotate( >>> speciality=Subquery(Director.objects.values('speciality')[:1]), >>> has_speciality=Q(speciality__isnull=False) >>> ).count() … AttributeError: 'WhereNode' object has no attribute 'is_summary'
Reverting this precise commit seems to fix the issue : https://github.com/django/django/commit/b64db05b9cedd96905d637a2d824cbbf428e40e7
The request is a bit exotic, I don't know if it should be supported.
This is the first ticket I open, I can clarify/submit a merge request if needed.
Change History (6)
comment:1 by , 2 years ago
comment:2 by , 2 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 2 years ago
Cc: | added |
---|
comment:4 by , 2 years ago
Here's a simple boiled down failing test case:
diff --git a/tests/queries/tests.py b/tests/queries/tests.py index 1bd72dd8b8..c6d9b05761 100644 --- a/tests/queries/tests.py +++ b/tests/queries/tests.py @@ -1312,6 +1312,10 @@ class Queries1Tests(TestCase): ) self.assertSequenceEqual(Note.objects.exclude(negate=True), [self.n3]) + def test_count_on_annotation(self): + # Ticket: #34024 + Tag.objects.annotate(has_pk=~Q(pk=None)).count() + class Queries2Tests(TestCase): @classmethod
results
E ====================================================================== ERROR: test_count_on_annotation (queries.tests.Queries1Tests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/davids/projects/django/tests/queries/tests.py", line 1317, in test_count_on_annotation Tag.objects.annotate(has_pk=~Q(pk=None)).count() File "/Users/davids/projects/django/django/db/models/query.py", line 621, in count return self.query.get_count(using=self.db) File "/Users/davids/projects/django/django/db/models/sql/query.py", line 554, in get_count return obj.get_aggregation(using, ["__count"])["__count"] File "/Users/davids/projects/django/django/db/models/sql/query.py", line 503, in get_aggregation if expression.is_summary: AttributeError: 'WhereNode' object has no attribute 'is_summary' ---------------------------------------------------------------------- Ran 1 test in 0.014s FAILED (errors=1) Destroying test database for alias 'default'...
Version 0, edited 2 years ago by (next)
comment:5 by , 2 years ago
Cc: | added |
---|---|
Has patch: | set |
Owner: | changed from | to
Status: | new → assigned |
Note:
See TracTickets
for help on using tickets.
Confirmed on latest main.
Full traceback from ipython: