Fixed #33670 -- Fixed altering primary key on SQLite.

This commit is contained in:
Mariusz Felisiak 2022-04-29 20:16:34 +02:00 committed by GitHub
parent 562e3bc09a
commit b34238addc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -217,7 +217,12 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
alter_field and getattr(alter_field[1], "primary_key", False)
):
for name, field in list(body.items()):
if field.primary_key:
if field.primary_key and not (
# Do not remove the old primary key when an altered field
# that introduces a primary key is the same field.
alter_field
and name == alter_field[1].name
):
field.primary_key = False
restore_pk_field = field
if field.auto_created:

View File

@ -3584,6 +3584,21 @@ class SchemaTests(TransactionTestCase):
)
self.assertEqual(self.get_primary_key(Tag._meta.db_table), "slug")
def test_alter_primary_key_the_same_name(self):
with connection.schema_editor() as editor:
editor.create_model(Thing)
old_field = Thing._meta.get_field("when")
new_field = CharField(max_length=2, primary_key=True)
new_field.set_attributes_from_name("when")
new_field.model = Thing
with connection.schema_editor() as editor:
editor.alter_field(Thing, old_field, new_field, strict=True)
self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
with connection.schema_editor() as editor:
editor.alter_field(Thing, new_field, old_field, strict=True)
self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
def test_context_manager_exit(self):
"""
Ensures transaction is correctly closed when an error occurs