1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #28541 -- Fixed migrations crash when changing primary key on SQLite.

This commit is contained in:
Ben Cail
2024-03-07 15:34:16 -05:00
committed by Mariusz Felisiak
parent 3d7235c67b
commit 593067a8ee
2 changed files with 44 additions and 0 deletions

View File

@@ -229,6 +229,14 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
body_copy["__module__"] = model.__module__
new_model = type("New%s" % model._meta.object_name, model.__bases__, body_copy)
# Remove the automatically recreated default primary key, if it has
# been deleted.
if delete_field and delete_field.attname == new_model._meta.pk.attname:
auto_pk = new_model._meta.pk
delattr(new_model, auto_pk.attname)
new_model._meta.local_fields.remove(auto_pk)
new_model.pk = None
# Create a new table with the updated schema.
self.create_model(new_model)