Hi folks,

I've noticed a difference in the way check constraint validation behaves
compared to the way Postgres (assuming similar behaviour in other DBs)
validates when dealing with NULLs.

Given a model with a simple positive check constraint:

class Test(Model):
    test = IntegerField(null=True)

    class Meta:
        constraints = [
           CheckConstraint(check=Q(test__gte=0), name="positive"),
        ]

Then the an instance with a NULL will save but fail validation:

test = Test()
test.save()
test.validate_constraints()
...
E           django.core.exceptions.ValidationError: {'__all__':
['Constraint “positive” is violated.']}


I believe this is due to the way validation is handled in the WHERE
clause. I've
trawled back through the PR discussion and original ticket as well as the
validation code – I could see the original comment about moving validation
to WHERE to get around issues with using SELECT but couldn't find any other
mention about how NULLs should be handled.

I wanted to check with folks here first before raising an issue. I'm not
sure what the fix should be but I'd recommend at least adding a note in the
docs if it's not fixable? :)

Cheers,
David

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CADyZw-4uYZnpWgjOXT-Wa2BSs%2BG1f4PNFD0YDU096bK9Uzc0tw%40mail.gmail.com.

Reply via email to