From 595a2abb58e04caa4d55fb2589bb80fb2a8fdfa1 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Fri, 7 Jul 2023 19:43:51 -0400 Subject: Fixed #34701 -- Added support for NULLS [NOT] DISTINCT on PostgreSQL 15+. --- tests/invalid_models_tests/test_models.py | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'tests/invalid_models_tests/test_models.py') diff --git a/tests/invalid_models_tests/test_models.py b/tests/invalid_models_tests/test_models.py index 9e2a37b79a..dc52f58c44 100644 --- a/tests/invalid_models_tests/test_models.py +++ b/tests/invalid_models_tests/test_models.py @@ -2753,6 +2753,52 @@ class ConstraintsTests(TestCase): self.assertEqual(Model.check(databases=self.databases), []) + def test_unique_constraint_nulls_distinct(self): + class Model(models.Model): + name = models.CharField(max_length=10) + + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["name"], + name="name_uq_distinct_null", + nulls_distinct=True, + ), + ] + + warn = Warning( + f"{connection.display_name} does not support unique constraints with nulls " + "distinct.", + hint=( + "A constraint won't be created. Silence this warning if you don't care " + "about it." + ), + obj=Model, + id="models.W047", + ) + expected = ( + [] + if connection.features.supports_nulls_distinct_unique_constraints + else [warn] + ) + self.assertEqual(Model.check(databases=self.databases), expected) + + def test_unique_constraint_nulls_distinct_required_db_features(self): + class Model(models.Model): + name = models.CharField(max_length=10) + + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["name"], + name="name_uq_distinct_null", + nulls_distinct=True, + ), + ] + required_db_features = {"supports_nulls_distinct_unique_constraints"} + + self.assertEqual(Model.check(databases=self.databases), []) + @skipUnlessDBFeature("supports_expression_indexes") def test_func_unique_constraint_expression_custom_lookup(self): class Model(models.Model): -- cgit v1.3