#33595: Check Constraint on nullable BooleanField should not be simplified
-------------------------------------+-------------------------------------
               Reporter:  Peter Law  |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  3.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          |
-------------------------------------+-------------------------------------
 Given a model like:

 {{{#!python
 class Foo(models.Model):
     it_exists = models.BooleanField(null=True)
     class Meta:
         constraints = [
             models.constraints.CheckConstraint(
                 check=models.Q(it_exists=True),
                 name="exists_constraint",
             ),
         ]
 }}}

 The constraint will allow both True and None values, yet that isn't at all
 obvious from the declaration.

 I'm using Postgres 11 and the resulting check is simplified (in terms of
 the SQL from the migration) down to something like `CHECK (("it_exists"))`
 (I've got a few other things in my actual constraint, but this what I see
 for the nullable boolean field).

 This appears to end up allowing database NULL values to act as truthy
 values, which is undesirable (even if something which SQL allows).

 I would have expected that the constraint would be more like `"it_exists"
 = TRUE`, which I don't think would allow NULL values to pass the check.

 It's possible to work around this by adding an explicit check (i.e: not-
 null-and-equals-true), though it would be great if the default behaviour
 was more in line with expectations.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33595>
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/0107017fb29b7e0c-d22b8ffd-2369-4d13-8865-5ad564f4e6f4-000000%40eu-central-1.amazonses.com.

Reply via email to