diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php index 2a486b1043..af8f46b153 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorageSchema.php @@ -1452,7 +1452,24 @@ protected function deleteSharedTableSchema(FieldStorageDefinitionInterface $stor if ($field_name == $deleted_field_name) { $schema = $this->getSharedTableFieldSchema($storage_definition, $table_name, $column_names); - // Drop indexes and unique keys first. + // Drop the primary key, indexes and unique keys first. + // The entity schema needs to be checked because the field schema is + // potentially incomplete. + // @todo Remove this in + // https://www.drupal.org/project/drupal/issues/2929120 + $entity_schema = $this->getEntitySchema($this->entityType); + if (array_intersect($column_names, $entity_schema[$table_name]['primary key'])) { + // Dropping a primary key must not leave a serial key, so we need to + // change any serial field to integer first. + foreach ($entity_schema[$table_name]['primary key'] as $column_name) { + if ($entity_schema[$table_name]['fields'][$column_name]['type'] === 'serial') { + $field_schema = $entity_schema[$table_name]['fields'][$column_name]; + $field_schema['type'] = 'int'; + $schema_handler->changeField($table_name, $column_name, $column_name, $field_schema); + } + } + $schema_handler->dropPrimaryKey($table_name); + } if (!empty($schema['indexes'])) { foreach ($schema['indexes'] as $name => $specifier) { $schema_handler->dropIndex($table_name, $name);