diff options
| author | saJaeHyukc <wogur981208@gmail.com> | 2025-03-10 22:55:05 +0900 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2025-03-10 20:11:01 +0100 |
| commit | c1257350ca118868bdfc867926dffad38b215162 (patch) | |
| tree | 57710fa895c7e03dc92ca634ef94134eccc8ff0d | |
| parent | e44e8327d3d88d86895735c0e427102063ff5b55 (diff) | |
Fixed #36222 -- Fixed ExclusionConstraint validation crash on excluded fields in condition.
Signed-off-by: saJaeHyukc <wogur981208@gmail.com>
| -rw-r--r-- | AUTHORS | 1 | ||||
| -rw-r--r-- | django/contrib/postgres/constraints.py | 5 | ||||
| -rw-r--r-- | tests/postgres_tests/test_constraints.py | 11 |
3 files changed, 17 insertions, 0 deletions
@@ -455,6 +455,7 @@ answer newbie questions, and generally made Django that much better: Jacob Kaplan-Moss <jacob@jacobian.org> Jacob Rief <jacob.rief@gmail.com> Jacob Walls <http://www.jacobtylerwalls.com/> + JaeHyuck Sa <wogur981208@gmail.com> Jakub BagiĆski <https://github.com/Jacob1507> Jakub Paczkowski <jakub@paczkowski.eu> Jakub Wilk <jwilk@jwilk.net> diff --git a/django/contrib/postgres/constraints.py b/django/contrib/postgres/constraints.py index ea06b10d12..e030c1190b 100644 --- a/django/contrib/postgres/constraints.py +++ b/django/contrib/postgres/constraints.py @@ -206,6 +206,11 @@ class ExclusionConstraint(BaseConstraint): self.get_violation_error_message(), code=self.violation_error_code ) else: + # Ignore constraints with excluded fields in condition. + if exclude and self._expression_refs_exclude( + model, self.condition, exclude + ): + return if (self.condition & Exists(queryset.filter(self.condition))).check( replacement_map, using=using ): diff --git a/tests/postgres_tests/test_constraints.py b/tests/postgres_tests/test_constraints.py index 96b5d39092..f107bffcfe 100644 --- a/tests/postgres_tests/test_constraints.py +++ b/tests/postgres_tests/test_constraints.py @@ -797,6 +797,17 @@ class ExclusionConstraintTests(PostgreSQLTestCase): ), exclude={"datespan", "start", "end", "room"}, ) + # Constraints with excluded fields in condition are ignored. + constraint.validate( + HotelReservation, + HotelReservation( + datespan=(datetimes[1].date(), datetimes[2].date()), + start=datetimes[1], + end=datetimes[2], + room=room102, + ), + exclude={"cancelled"}, + ) def test_range_overlaps_custom(self): class TsTzRange(Func): |
