#34754: CheckConstraint with isnull lookup on JSONField transform None into null
jsonb value
-------------------------------------+-------------------------------------
Reporter: Alexandre | Owner: nobody
Collet |
Type: | Status: new
Uncategorized |
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords: constraint,
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/34754>
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/01070189b18c1e55-82b29922-fcbf-4336-94ee-5ec6e9bd4f78-000000%40eu-central-1.amazonses.com.