[Django] #33895: Syntax error when doing annotate with particular Q object

12 views
Skip to first unread message

Django

unread,
Aug 4, 2022, 6:03:41 PM8/4/22
to django-...@googlegroups.com
#33895: Syntax error when doing annotate with particular Q object
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
shukryzablah |
Type: Bug | Status: new
Component: Database | Version: 2.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The following code generates a syntax error.

{{{
User.objects.annotate(
_a=Case(
When(~Q(pk__in=[]), then=Value(True)),
default=Value(False),
output_field=BooleanField(),
)
).order_by("-a").values("pk")
}}}

The error is:


{{{
ProgrammingError: syntax error at or near "THEN"
LINE 1: ..._user"."id" FROM "users_user" ORDER BY CASE WHEN THEN true ...
}}}

The generated SQL is:


{{{
SELECT "users_user"."id" FROM "users_user" ORDER BY CASE WHEN THEN True
ELSE False END ASC
}}}


I expected behavior to annotate all rows with the value True since they
all match.

Relevant because ~Q(pk__in=[]) is a sentinel value that is sometimes
returned by application code.

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

Django

unread,
Aug 4, 2022, 6:05:59 PM8/4/22
to django-...@googlegroups.com
#33895: Syntax error when doing annotate with particular Q object
-------------------------------------+-------------------------------------
Reporter: shukryzablah | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

* cc: shukryzablah (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/33895#comment:1>

Reply all
Reply to author
Forward
0 new messages