﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34754	CheckConstraint with isnull lookup on JSONField transform None into null jsonb value	Alexandre Collet	Simon Charette	"
Hello !

I think i've found a bug in the validation of constraints for the JSONField with the isnull lookup (with postgresql). 
Let me explain:

I have a model with some values stored in a JSONField and i want to be sure (with a CheckConstraint) than if this field was not None, an author was set.

{{{
class MyModel(Model, UuidMixin, TimestampableMixin):
    values = JSONField(
        null=True,
        blank=True,
    )
    author = ForeignKey(
        null=True,
        blank=True,
        to=settings.AUTH_USER_MODEL,
        on_delete=SET_NULL,
    )

    class Meta:
        constraints = [
            CheckConstraint(
                name=""author_cannot_be_null_if_values_was_set"",
                check=(
                    Q(values__isnull=True)
                    | Q(values__isnull=False, author__isnull=False)
                ),
            ),
        ]
}}}

This works perfectly in 4.1. 
But in 4.2, the sql generated for the check when i does not set the field ""values"" and ""author"" is that:
{{{
instance = MyModel(values=None, author=None)
instance.validate_constraints() 
}}}
{{{
 SELECT 1 AS ""_check"" WHERE COALESCE((('null'::jsonb IS NULL OR (NULL IS NOT NULL AND 'null'::jsonb IS NOT NULL))), true)
}}}

This raise a ValidationError because the None value was transformed into a jsonb null value and the isnull lookup is always False.

I'm not sure how to correct this, but if anyone would like to help me, I'd be delighted to make a pull request.


"	Bug	closed	Database layer (models, ORM)	4.2	Release blocker	fixed	constraint, jsonfield, none, check	Simon Charette	Ready for checkin	1	0	0	0	0	0
