diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2024-03-26 22:58:47 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-26 22:58:47 +0100 |
| commit | b98271a6e42107233311d17f5d7bc74fbb47f22c (patch) | |
| tree | 5714afddd37718c68ef1d4e8e1caacbaddfc5c28 /tests/schema | |
| parent | ae10146793dca0e0594c7acdee20ca3810983f39 (diff) | |
Fixed #35329 -- Fixed migrations crash when adding partial unique constraints with nulls_distinct.
Bug in 595a2abb58e04caa4d55fb2589bb80fb2a8fdfa1.
Thanks Lucas Lemke Saunitti for the report.
Diffstat (limited to 'tests/schema')
| -rw-r--r-- | tests/schema/tests.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py index b912d353eb..f8e314d270 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -3629,6 +3629,38 @@ class SchemaTests(TransactionTestCase): constraints = self.get_constraints(Author._meta.db_table) self.assertNotIn(constraint.name, constraints) + @skipUnlessDBFeature( + "supports_nulls_distinct_unique_constraints", + "supports_partial_indexes", + ) + def test_unique_constraint_nulls_distinct_condition(self): + with connection.schema_editor() as editor: + editor.create_model(Author) + constraint = UniqueConstraint( + fields=["height", "weight"], + name="un_height_weight_start_A", + condition=Q(name__startswith="A"), + nulls_distinct=False, + ) + with connection.schema_editor() as editor: + editor.add_constraint(Author, constraint) + Author.objects.create(name="Adam", height=None, weight=None) + Author.objects.create(name="Avocado", height=1, weight=None) + Author.objects.create(name="Adrian", height=None, weight=1) + with self.assertRaises(IntegrityError): + Author.objects.create(name="Alex", height=None, weight=None) + Author.objects.create(name="Bob", height=None, weight=None) + with self.assertRaises(IntegrityError): + Author.objects.create(name="Alex", height=1, weight=None) + Author.objects.create(name="Bill", height=None, weight=None) + with self.assertRaises(IntegrityError): + Author.objects.create(name="Alex", height=None, weight=1) + Author.objects.create(name="Celine", height=None, weight=1) + with connection.schema_editor() as editor: + editor.remove_constraint(Author, constraint) + constraints = self.get_constraints(Author._meta.db_table) + self.assertNotIn(constraint.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 |
