Opened 19 months ago

Last modified 6 weeks ago

#34059 closed New feature

Validation of check constraints on postgres json fields produce invalid SQL — at Version 1

Reported by: Dan LaManna Owned by:
Component: Database layer (models, ORM) Version: 4.1
Severity: Normal Keywords:
Cc: David Sanders Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Dan LaManna)

Given a model with constraints within the JSONField:

class Version(models.Model):
    metadata = models.JSONField(blank=True, default=dict)

    class Meta:
        constraints = [
            models.CheckConstraint(
                name='version_metadata_has_schema_version',
                check=~Q(metadata__schemaVersion=None),
            )
        ]

The following code produces an error:

version = Version(metadata={'foo': 'bar'})
version.validate_constraints()
WARNING  Got a database error calling check() on <Q: (AND: (NOT (AND: ('metadata__schemaVersion', None))))>: operator is not unique: unknown -> unknown         
                    LINE 1: SELECT 1 AS "_check" WHERE NOT (('{"foo": "bar"}' -> 'schema...                                                                                                  
                                                                              ^                                                                                                              
                    HINT:  Could not choose a best candidate operator. You might need to add explicit type casts

Internally it's running the following query:

SELECT 1 AS "_check"
 WHERE NOT (('{"foo": "bar"}' -> 'schemaVersion') = 'null')

This appears similar to #33905.

Change History (1)

comment:1 by Dan LaManna, 19 months ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top