[Django] #32007: AttributeError: 'WhereNode' object has no attribute 'copy'

46 views
Skip to first unread message

Django

unread,
Sep 14, 2020, 3:55:34 PM9/14/20
to django-...@googlegroups.com
#32007: AttributeError: 'WhereNode' object has no attribute 'copy'
------------------------------------------+------------------------
Reporter: Gordon Wrigley | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+------------------------
I think I've found a regression in 3.1, I'm seeing this across databases
and Python versions but only on Django 3.1.
With reference to the polls app the queryset I hit this on is essentially:
{{{#!python
Question.objects.values("question_text").annotate(
pub_date__count=Count("pub_date", distinct=True),
pub_date__is_null=ExpressionWrapper(Q(pub_date=None),
output_field=BooleanField())
).values("question_text", "pub_date__count", "pub_date__is_null")
}}}
This results in:
{{{
~\.venv\django_tutorial\lib\site-packages\django\db\models\query.py in
annotate(self, *args, **kwargs)
1121 clone.query.group_by = True
1122 else:
-> 1123 clone.query.set_group_by()
1124 break
1125

~\.venv\django_tutorial\lib\site-packages\django\db\models\sql\query.py in
set_group_by(self, allow_aliases)
1979 if not allow_aliases or alias in column_names:
1980 alias = None
-> 1981 group_by_cols =
annotation.get_group_by_cols(alias=alias)
1982 group_by.extend(group_by_cols)
1983 self.group_by = tuple(group_by)

~\.venv\django_tutorial\lib\site-packages\django\db\models\expressions.py
in get_group_by_cols(self, alias)
867
868 def get_group_by_cols(self, alias=None):
--> 869 expression = self.expression.copy()
870 expression.output_field = self.output_field
871 return expression.get_group_by_cols(alias=alias)

AttributeError: 'WhereNode' object has no attribute 'copy'
}}}

It's a bit of a dumb query but given my usecase https://pypi.org/project
/django-data-browser/ that's a little beyond my control.

Still it's kinda surprising that it worked on 3.0 and doesn't on 3.1.

It can be simplified a bit and still get essentially the same result:
{{{#!python
Question.objects.annotate(
pub_date__count=Count("pub_date", distinct=True),
pub_date__is_null=ExpressionWrapper(Q(pub_date=None),
output_field=BooleanField())
)
}}}

Removing either of the annotate clauses "fixes" it.

--
Ticket URL: <https://code.djangoproject.com/ticket/32007>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 15, 2020, 3:56:20 AM9/15/20
to django-...@googlegroups.com
#32007: QuerySet.annotate() with WhereNode() crashes with AttributeError.
-------------------------------------+-------------------------------------

Reporter: Gordon Wrigley | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Gordon Wrigley):

Here is a more useful variant, this is a type thing I actually do on a
regular basis, and I guess also means work won't be upgrading to 3.1 until
I find a work around.

{{{#!python
Question.objects.annotate(
pub_date__is_null=ExpressionWrapper(Q(pub_date=None),
output_field=BooleanField())
).values("pub_date__is_null").annotate(
id__count=Count("id", distinct=True)
).values("pub_date__is_null", "id__count")
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32007#comment:3>

Django

unread,
Sep 15, 2020, 3:59:23 AM9/15/20
to django-...@googlegroups.com
#32007: QuerySet.annotate() with WhereNode() crashes with AttributeError.
-------------------------------------+-------------------------------------
Reporter: Gordon Wrigley | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Gordon Wrigley):

Also as a total aside regarding that last snippet it'd be nice if Q
objects just were boolean expressions.

--
Ticket URL: <https://code.djangoproject.com/ticket/32007#comment:4>

Django

unread,
Sep 15, 2020, 4:32:57 AM9/15/20
to django-...@googlegroups.com
#32007: QuerySet.annotate() with WhereNode() crashes with AttributeError.
-------------------------------------+-------------------------------------
Reporter: Gordon Wrigley | Owner: felixxm
Type: Bug | Status: assigned

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* owner: nobody => felixxm
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/32007#comment:5>

Django

unread,
Sep 15, 2020, 4:56:48 AM9/15/20
to django-...@googlegroups.com
#32007: QuerySet.annotate() with WhereNode() crashes with AttributeError.
-------------------------------------+-------------------------------------
Reporter: Gordon Wrigley | Owner: felixxm
Type: Bug | Status: assigned
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/13422 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/32007#comment:6>

Django

unread,
Sep 15, 2020, 5:41:21 AM9/15/20
to django-...@googlegroups.com
#32007: QuerySet.annotate() with WhereNode() crashes with AttributeError.
-------------------------------------+-------------------------------------
Reporter: Gordon Wrigley | Owner: felixxm
Type: Bug | Status: closed

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"eaf9764d3bb25970da89de5799d8d308715628ba" eaf9764d]:
{{{
#!CommitTicketReference repository=""
revision="eaf9764d3bb25970da89de5799d8d308715628ba"
Fixed #32007 -- Fixed queryset crash with Q() annotation and aggregation.

Thanks Gordon Wrigley for the report.

Regression in 8a6df55f2dd5131282084a4edfd48f63fbf8c69a.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32007#comment:7>

Django

unread,
Sep 15, 2020, 5:42:09 AM9/15/20
to django-...@googlegroups.com
#32007: QuerySet.annotate() with WhereNode() crashes with AttributeError.
-------------------------------------+-------------------------------------
Reporter: Gordon Wrigley | Owner: felixxm
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"1afc9b31bb4f3f5f921060ca42d76aed5edda6e3" 1afc9b3]:
{{{
#!CommitTicketReference repository=""
revision="1afc9b31bb4f3f5f921060ca42d76aed5edda6e3"
[3.1.x] Fixed #32007 -- Fixed queryset crash with Q() annotation and
aggregation.

Thanks Gordon Wrigley for the report.

Regression in 8a6df55f2dd5131282084a4edfd48f63fbf8c69a.
Backport of eaf9764d3bb25970da89de5799d8d308715628ba from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32007#comment:8>

Django

unread,
Sep 16, 2020, 5:47:21 AM9/16/20
to django-...@googlegroups.com
#32007: QuerySet.annotate() with WhereNode() crashes with AttributeError.
-------------------------------------+-------------------------------------
Reporter: Gordon Wrigley | Owner: felixxm
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by GitHub <noreply@…>):

In [changeset:"3a9f192b131f7a9b0fe5783c684b23015fa67cc8" 3a9f192]:
{{{
#!CommitTicketReference repository=""
revision="3a9f192b131f7a9b0fe5783c684b23015fa67cc8"
Refs #32007 -- Skipped test_q_expression_annotation_with_aggregation on
Oracle.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32007#comment:9>

Django

unread,
Sep 16, 2020, 5:48:16 AM9/16/20
to django-...@googlegroups.com
#32007: QuerySet.annotate() with WhereNode() crashes with AttributeError.
-------------------------------------+-------------------------------------
Reporter: Gordon Wrigley | Owner: felixxm
Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"5a03e14deb55c381b2908fdeff884137ca8e43a4" 5a03e14d]:
{{{
#!CommitTicketReference repository=""
revision="5a03e14deb55c381b2908fdeff884137ca8e43a4"
[3.1.x] Refs #32007 -- Skipped
test_q_expression_annotation_with_aggregation on Oracle.

Backport of 3a9f192b131f7a9b0fe5783c684b23015fa67cc8 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32007#comment:10>

Django

unread,
Nov 16, 2020, 5:30:20 PM11/16/20
to django-...@googlegroups.com
#32007: QuerySet.annotate() with WhereNode() crashes with AttributeError.
-------------------------------------+-------------------------------------
Reporter: Gordon Wrigley | Owner: Mariusz
| Felisiak

Type: Bug | Status: closed
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Gordon Wrigley):

I think this is related to https://code.djangoproject.com/ticket/32200

--
Ticket URL: <https://code.djangoproject.com/ticket/32007#comment:11>

Reply all
Reply to author
Forward
0 new messages