summaryrefslogtreecommitdiff
path: root/tests/schema/tests.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2023-07-07 19:43:51 -0400
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-07-19 21:42:27 +0200
commit595a2abb58e04caa4d55fb2589bb80fb2a8fdfa1 (patch)
treef08cf5a71a9d637818d896a7b31b2d7860e336ca /tests/schema/tests.py
parent98cfb90182a8baa806fc4e09e294b6cfc5d09eff (diff)
Fixed #34701 -- Added support for NULLS [NOT] DISTINCT on PostgreSQL 15+.
Diffstat (limited to 'tests/schema/tests.py')
-rw-r--r--tests/schema/tests.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 688a9f1fcf..5c20155387 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -3318,6 +3318,43 @@ class SchemaTests(TransactionTestCase):
with self.assertRaises(DatabaseError):
editor.add_constraint(Author, constraint)
+ @skipUnlessDBFeature("supports_nulls_distinct_unique_constraints")
+ def test_unique_constraint_nulls_distinct(self):
+ with connection.schema_editor() as editor:
+ editor.create_model(Author)
+ nulls_distinct = UniqueConstraint(
+ F("height"), name="distinct_height", nulls_distinct=True
+ )
+ nulls_not_distinct = UniqueConstraint(
+ F("weight"), name="not_distinct_weight", nulls_distinct=False
+ )
+ with connection.schema_editor() as editor:
+ editor.add_constraint(Author, nulls_distinct)
+ editor.add_constraint(Author, nulls_not_distinct)
+ Author.objects.create(name="", height=None, weight=None)
+ Author.objects.create(name="", height=None, weight=1)
+ with self.assertRaises(IntegrityError):
+ Author.objects.create(name="", height=1, weight=None)
+ with connection.schema_editor() as editor:
+ editor.remove_constraint(Author, nulls_distinct)
+ editor.remove_constraint(Author, nulls_not_distinct)
+ constraints = self.get_constraints(Author._meta.db_table)
+ self.assertNotIn(nulls_distinct.name, constraints)
+ self.assertNotIn(nulls_not_distinct.name, constraints)
+
+ @skipIfDBFeature("supports_nulls_distinct_unique_constraints")
+ def test_unique_constraint_nulls_distinct_unsupported(self):
+ # UniqueConstraint is ignored on databases that don't support
+ # NULLS [NOT] DISTINCT.
+ with connection.schema_editor() as editor:
+ editor.create_model(Author)
+ constraint = UniqueConstraint(
+ F("name"), name="func_name_uq", nulls_distinct=True
+ )
+ with connection.schema_editor() as editor, self.assertNumQueries(0):
+ self.assertIsNone(editor.add_constraint(Author, constraint))
+ self.assertIsNone(editor.remove_constraint(Author, constraint))
+
@ignore_warnings(category=RemovedInDjango51Warning)
def test_index_together(self):
"""