Fixed #28646 -- Prevented duplicate index when unique is set to True on PostgreSQL.

This commit is contained in:
Ben Cail 2024-02-01 16:08:42 -05:00 committed by Sarah Boyce
parent 7e00fee3bd
commit 9cf9c796be
6 changed files with 305 additions and 11 deletions

View file

@ -1,4 +1,5 @@
import math
import unittest
from decimal import Decimal
from django.core.exceptions import FieldDoesNotExist
@ -6213,6 +6214,52 @@ class OperationTests(OperationTestBase):
self.assertEqual(pony_new.static, 2)
class PrimaryKeyOperations(OperationTestBase):
@unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
def test_slugfields_change_primary_key_operations(self):
operation1 = migrations.CreateModel(
"SimpleModel",
[
("field1", models.SlugField(max_length=20, primary_key=True)),
("field2", models.SlugField(max_length=20)),
],
)
operation2 = migrations.AlterField(
"SimpleModel",
"field1",
models.SlugField(max_length=20, primary_key=False),
)
operation3 = migrations.AlterField(
"SimpleModel",
"field2",
models.SlugField(max_length=20, primary_key=True),
)
project_state = ProjectState()
new_state = project_state.clone()
operation1.state_forwards("migrtest", new_state)
self.assertTableNotExists("migrtest_simplemodel")
with connection.schema_editor() as editor:
operation1.state_forwards("migrtest", new_state)
operation1.database_forwards("migrtest", editor, project_state, new_state)
project_state, new_state = new_state, new_state.clone()
operation2.state_forwards("migrtest", new_state)
operation2.database_forwards("migrtest", editor, project_state, new_state)
project_state, new_state = new_state, new_state.clone()
operation3.state_forwards("migrtest", new_state)
operation3.database_forwards("migrtest", editor, project_state, new_state)
self.assertTableExists("migrtest_simplemodel")
self.assertColumnExists("migrtest_simplemodel", "field1")
self.assertColumnExists("migrtest_simplemodel", "field2")
with connection.cursor() as cursor:
primary_keys = connection.introspection.get_primary_key_columns(
cursor, "migrtest_simplemodel"
)
self.assertEqual(["field2"], primary_keys)
self.assertIndexExists("migrtest_simplemodel", ["field1"], index_type="idx")
self.assertIndexExists("migrtest_simplemodel", ["field2"], index_type="idx")
class SwappableOperationTests(OperationTestBase):
"""
Key operations ignore swappable models