#30841: Prevent using  __isnull lookup with non-boolean value.
-------------------------------------+-------------------------------------
     Reporter:  André Ericson        |                    Owner:  André
         Type:                       |  Ericson
  Cleanup/optimization               |                   Status:  new
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |
     Severity:  Normal               |               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):

 * status:  closed => new
 * type:  New feature => Cleanup/optimization
 * has_patch:  1 => 0
 * resolution:  wontfix =>


Old description:

> Using `isnull` filter with a non-boolean value will not promote `INNER
> JOIN` to an `OUTER JOIN` leading to inconsistent behaviour.
>
> I'm submitting a PR together with this ticket and will link it here.

New description:

 `__isnull` should not allow for non-boolean values. Using truthy/falsey
 doesn't promote `INNER JOIN` to an `OUTER JOIN` but works fine for a
 simple queries. Using non-boolean values is
 [https://docs.djangoproject.com/en/2.2/ref/models/querysets/#isnull
 undocumented] and untested. IMO we should raise an error for non-boolean
 values to avoid confusion and for consistency.

--

Comment:

 Agreed, we should raise an error for non-boolean values, e.g.
 {{{
 diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py
 index 9344979c56..fc4a38c4fe 100644
 --- a/django/db/models/lookups.py
 +++ b/django/db/models/lookups.py
 @@ -463,6 +463,11 @@ class IsNull(BuiltinLookup):
      prepare_rhs = False

      def as_sql(self, compiler, connection):
 +        if not isinstance(self.rhs, bool):
 +            raise ValueError(
 +                'The QuerySet value for an isnull lookup must be True or
 '
 +                'False.'
 +            )
          sql, params = compiler.compile(self.lhs)
          if self.rhs:
              return "%s IS NULL" % sql, params
 }}}

 I changed the ticket description.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30841#comment:8>
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/066.8595de733ccd48fbdde5e83b66e49e14%40djangoproject.com.

Reply via email to