0
0
mirror of https://github.com/django/django.git synced 2024-11-21 19:09:18 +01:00

fix sqlite error when removing primary key field

This commit is contained in:
Ben Cail 2024-06-14 09:19:09 -04:00
parent 37acffbd6a
commit ca2d47af1d
2 changed files with 25 additions and 8 deletions

View File

@ -243,15 +243,16 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
self.create_model(new_model)
# Copy data from the old table into the new table
self.execute(
"INSERT INTO %s (%s) SELECT %s FROM %s"
% (
self.quote_name(new_model._meta.db_table),
", ".join(self.quote_name(x) for x in mapping),
", ".join(mapping.values()),
self.quote_name(model._meta.db_table),
if mapping:
self.execute(
"INSERT INTO %s (%s) SELECT %s FROM %s"
% (
self.quote_name(new_model._meta.db_table),
", ".join(self.quote_name(x) for x in mapping),
", ".join(mapping.values()),
self.quote_name(model._meta.db_table),
)
)
)
# Delete the old table to make way for the new
self.delete_model(model, handle_autom2m=False)

View File

@ -2330,6 +2330,22 @@ class SchemaTests(TransactionTestCase):
# Ensure the foreign key reference was updated.
self.assertForeignKeyExists(Book, "author_id", "schema_author", "renamed")
@isolate_apps("schema")
def test_remove_primary_key_from_model(self):
class Author(Model):
name = CharField(max_length=255, primary_key=True)
class Meta:
app_label = "schema"
with connection.schema_editor() as editor:
editor.create_model(Author)
with connection.schema_editor() as editor:
editor.remove_field(Author, Author._meta.get_field("name"))
columns = self.column_classes(Author)
self.assertNotIn("name", columns)
@skipIfDBFeature("interprets_empty_strings_as_nulls")
def test_rename_keep_null_status(self):
"""