From f030236a86a64a4befd3cc8093e2bbeceef52a31 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Sun, 12 May 2024 20:10:55 +0200 Subject: Fixed #35275 -- Fixed Meta.constraints validation crash on UniqueConstraint with OpClass(). This also introduces Expression.constraint_validation_compatible that allows specifying that expression should be ignored during a constraint validation. --- django/contrib/postgres/constraints.py | 11 ++++------- django/contrib/postgres/indexes.py | 1 + 2 files changed, 5 insertions(+), 7 deletions(-) (limited to 'django/contrib/postgres') diff --git a/django/contrib/postgres/constraints.py b/django/contrib/postgres/constraints.py index ff702c53b0..a31f657183 100644 --- a/django/contrib/postgres/constraints.py +++ b/django/contrib/postgres/constraints.py @@ -1,6 +1,5 @@ from types import NoneType -from django.contrib.postgres.indexes import OpClass from django.core.exceptions import ValidationError from django.db import DEFAULT_DB_ALIAS, NotSupportedError from django.db.backends.ddl_references import Expressions, Statement, Table @@ -208,12 +207,10 @@ class ExclusionConstraint(BaseConstraint): if isinstance(expr, F) and expr.name in exclude: return rhs_expression = expression.replace_expressions(replacements) - # Remove OpClass because it only has sense during the constraint - # creation. - if isinstance(expression, OpClass): - expression = expression.get_source_expressions()[0] - if isinstance(rhs_expression, OpClass): - rhs_expression = rhs_expression.get_source_expressions()[0] + if hasattr(expression, "get_expression_for_validation"): + expression = expression.get_expression_for_validation() + if hasattr(rhs_expression, "get_expression_for_validation"): + rhs_expression = rhs_expression.get_expression_for_validation() lookup = PostgresOperatorLookup(lhs=expression, rhs=rhs_expression) lookup.postgres_operator = operator lookups.append(lookup) diff --git a/django/contrib/postgres/indexes.py b/django/contrib/postgres/indexes.py index cc944ed335..05fdbeed5e 100644 --- a/django/contrib/postgres/indexes.py +++ b/django/contrib/postgres/indexes.py @@ -244,6 +244,7 @@ class SpGistIndex(PostgresIndex): class OpClass(Func): template = "%(expressions)s %(name)s" + constraint_validation_compatible = False def __init__(self, expression, name): super().__init__(expression, name=name) -- cgit v1.3