#34254: Exists annotations can return non-boolean results (i.e. None) if used 
with
an empty QuerySet.
-------------------------------------+-------------------------------------
     Reporter:  Keryn Knight         |                    Owner:
                                     |  rajdesai24
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  Exists               |             Triage Stage:  Accepted
  EmptyQuerySet sqlite postgres      |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Keryn Knight):

 Replying to [comment:1 Simon Charette]:

 > I suspect the solution is as simple as setting
 `Exists.empty_result_set_value = False` [...]
 > [...]
 > If you can confirm this addresses the problem you reported [...]

 Confirming, it does seem to be as simple as that (a pleasant rarity in ORM
 edge cases, I'm sure!) for the 2 backends I was testing (`postgres` and
 `sqlite` -- I've notably not checked `mysql` or `oracle` etc):

 {{{
 In [3]:
 
A.objects.using("sqlite").annotate(should_be_bool=Exists(B.objects.none())).first().should_be_bool
 Out[3]: False

 In [4]:
 
A.objects.using("pg").annotate(should_be_bool=Exists(B.objects.none())).first().should_be_bool
 Out[4]: False
 }}}

 The queries are still recorded as different, but I expect that's normal
 coercion differences between sqlite & postgres:
 {{{
 In [6]: connections['sqlite'].queries
 Out[6]:
 [{'sql': 'SELECT "app_a"."id", 0 AS "should_be_bool" FROM "app_a" ORDER BY
 "app_a"."id" ASC LIMIT 1',
   'time': '0.001'}]

 In [7]: connections['pg'].queries
 Out[7]:
 [{'sql': 'SELECT "app_a"."id", false AS "should_be_bool" FROM "app_a"
 ORDER BY "app_a"."id" ASC LIMIT 1',
   'time': '0.001'}]
 }}}

 ---------------

 WRT to fixing it, as rajdesai24 has expressed an interest in doing so,
 I'll let them take the lead and get the contribution under their belt.

 Replying to [comment:3 rajdesai24]:
 > Hey Simon would love to submit a PR

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34254#comment:4>
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/01070185ba7ca48f-15f16ecb-1fc9-4edc-85fe-58d56f6e0bb8-000000%40eu-central-1.amazonses.com.

Reply via email to