diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index c5b428fc67..c6c81f5d40 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -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) diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 33a4bc527b..c47e0956b0 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -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): """