From aed4ffe1897c51c0dfd8689c0834ca4ca0a8a03e Mon Sep 17 00:00:00 2001 From: David Sanders Date: Mon, 5 Aug 2024 08:22:29 +0200 Subject: [5.1.x] Fixed #35638 -- Updated validate_constraints to consider db_default. Backport of 509763c79952cde02d9f5b584af4278bdbed77b2 from main. --- tests/postgres_tests/migrations/0002_create_test_models.py | 2 +- tests/postgres_tests/models.py | 2 +- tests/postgres_tests/test_constraints.py | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) (limited to 'tests/postgres_tests') diff --git a/tests/postgres_tests/migrations/0002_create_test_models.py b/tests/postgres_tests/migrations/0002_create_test_models.py index 5538b436ad..188f79607d 100644 --- a/tests/postgres_tests/migrations/0002_create_test_models.py +++ b/tests/postgres_tests/migrations/0002_create_test_models.py @@ -434,7 +434,7 @@ class Migration(migrations.Migration): primary_key=True, ), ), - ("ints", IntegerRangeField(null=True, blank=True)), + ("ints", IntegerRangeField(null=True, blank=True, db_default=(5, 10))), ("bigints", BigIntegerRangeField(null=True, blank=True)), ("decimals", DecimalRangeField(null=True, blank=True)), ("timestamps", DateTimeRangeField(null=True, blank=True)), diff --git a/tests/postgres_tests/models.py b/tests/postgres_tests/models.py index a97894e327..e3118bc590 100644 --- a/tests/postgres_tests/models.py +++ b/tests/postgres_tests/models.py @@ -130,7 +130,7 @@ class LineSavedSearch(PostgreSQLModel): class RangesModel(PostgreSQLModel): - ints = IntegerRangeField(blank=True, null=True) + ints = IntegerRangeField(blank=True, null=True, db_default=(5, 10)) bigints = BigIntegerRangeField(blank=True, null=True) decimals = DecimalRangeField(blank=True, null=True) timestamps = DateTimeRangeField(blank=True, null=True) diff --git a/tests/postgres_tests/test_constraints.py b/tests/postgres_tests/test_constraints.py index 3cc76cdcfe..9a572c9bf2 100644 --- a/tests/postgres_tests/test_constraints.py +++ b/tests/postgres_tests/test_constraints.py @@ -1238,3 +1238,12 @@ class ExclusionConstraintTests(PostgreSQLTestCase): constraint_name, self.get_constraints(ModelWithExclusionConstraint._meta.db_table), ) + + def test_database_default(self): + constraint = ExclusionConstraint( + name="ints_equal", expressions=[("ints", RangeOperators.EQUAL)] + ) + RangesModel.objects.create() + msg = "Constraint “ints_equal” is violated." + with self.assertRaisesMessage(ValidationError, msg): + constraint.validate(RangesModel, RangesModel()) -- cgit v1.3