Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33895 closed Bug (fixed)

Case() crashes with ~Q(pk__in=[]).

Reported by: shukryzablah Owned by: David Wobrock
Component: Database layer (models, ORM) Version: 2.2
Severity: Normal Keywords:
Cc: shukryzablah, David Wobrock, Johannes Dollinger Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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(pkin=[]) is a sentinel value that is sometimes returned by application code.

Change History (7)

comment:1 by shukryzablah, 2 years ago

Cc: shukryzablah added

comment:2 by Mariusz Felisiak, 2 years ago

Cc: David Wobrock added
Summary: Syntax error when doing annotate with particular Q objectCase() crashes with ~Q(pk__in=[]).
Triage Stage: UnreviewedAccepted

Thanks for the report (similar to the #33374).

Version 0, edited 2 years ago by Mariusz Felisiak (next)

comment:3 by Mariusz Felisiak, 2 years ago

Cc: Johannes Dollinger added

comment:4 by David Wobrock, 2 years ago

Has patch: set
Owner: changed from nobody to David Wobrock
Status: newassigned

comment:5 by Mariusz Felisiak, 2 years ago

Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 036bb2b7:

Fixed #33895 -- Fixed Case() crash with filters that match everything in When().

Thanks shukryzablah for the report.

comment:7 by GitHub <noreply@…>, 2 years ago

In 721706f:

Refs #33895 -- Fixed Case() crash with filters that match everything in When() on Oracle.

Follow up to 036bb2b74afb50a71e7a945e8dd83499b603ac3a.

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