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