summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
diff options
context:
space:
mode:
Diffstat (limited to 'core/lib/Drupal/Core/Database/Driver/mysql/Schema.php')
-rw-r--r--core/lib/Drupal/Core/Database/Driver/mysql/Schema.php12
1 files changed, 11 insertions, 1 deletions
diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
index 5dcd487..1911d6a 100644
--- a/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
+++ b/core/lib/Drupal/Core/Database/Driver/mysql/Schema.php
@@ -395,14 +395,24 @@ class Schema extends DatabaseSchema {
throw new SchemaObjectExistsException(t("Cannot add field @table.@field: field already exists.", ['@field' => $field, '@table' => $table]));
}
+ // Fields that are part of a PRIMARY KEY must be added as NOT NULL.
+ $is_primary_key = isset($keys_new['primary key']) && in_array($field, $keys_new['primary key'], TRUE);
+
$fixnull = FALSE;
- if (!empty($spec['not null']) && !isset($spec['default'])) {
+ if (!empty($spec['not null']) && !isset($spec['default']) && !$is_primary_key) {
$fixnull = TRUE;
$spec['not null'] = FALSE;
}
$query = 'ALTER TABLE {' . $table . '} ADD ';
$query .= $this->createFieldSql($field, $this->processField($spec));
if ($keys_sql = $this->createKeysSql($keys_new)) {
+ // Make sure to drop the existing primary key before adding a new one.
+ // This is only needed when adding a field because this method, unlike
+ // changeField(), is supposed to handle primary keys automatically.
+ if (isset($keys_new['primary key']) && $this->indexExists($table, 'PRIMARY')) {
+ $query .= ', DROP PRIMARY KEY';
+ }
+
$query .= ', ADD ' . implode(', ADD ', $keys_sql);
}
$this->connection->query($query);