#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/010701826ae2b38c-31024524-a61b-4c5b-9775-0a81dfccbd84-000000%40eu-central-1.amazonses.com.

Reply via email to