diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php index 97358dcf4e..11782fc1ca 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php @@ -189,7 +189,7 @@ public static function open(array &$connection_options = []) { ]; $connection_options['init_commands'] += [ - 'sql_mode' => "SET sql_mode = 'ANSI,TRADITIONAL,NO_AUTO_VALUE_ON_ZERO'", + 'sql_mode' => "SET sql_mode = 'ANSI,TRADITIONAL'", ]; // Execute initial commands. diff --git a/core/modules/user/src/UserStorage.php b/core/modules/user/src/UserStorage.php index eb47af1539..15bb6147da 100644 --- a/core/modules/user/src/UserStorage.php +++ b/core/modules/user/src/UserStorage.php @@ -2,6 +2,7 @@ namespace Drupal\user; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\Sql\SqlContentEntityStorage; use Drupal\Core\Session\AccountInterface; @@ -13,6 +14,28 @@ */ class UserStorage extends SqlContentEntityStorage implements UserStorageInterface { + /** + * {@inheritdoc} + */ + protected function doSaveFieldItems(ContentEntityInterface $entity, array $names = []) { + // The anonymous user account is saved with the fixed user ID of 0. MySQL + // does not support inserting an ID of 0 into serial field unless the SQL + // mode is set to NO_AUTO_VALUE_ON_ZERO. + if ($entity->id() === 0) { + $database = \Drupal::database(); + if ($database->databaseType() === 'mysql') { + $current_sql_mode = $database->query("SELECT @@sql_mode;")->fetchField(); + $database->query("SET sql_mode = '$current_sql_mode,NO_AUTO_VALUE_ON_ZERO'"); + } + } + parent::doSaveFieldItems($entity, $names); + + // Reset the SQL mode if we've changed it. + if (isset($current_sql_mode, $database)) { + $database->query("SET sql_mode = '$current_sql_mode'"); + } + } + /** * {@inheritdoc} */ diff --git a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php index fc0a7a26a5..0b163a2405 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SchemaTest.php @@ -709,26 +709,16 @@ protected function assertFieldAdditionRemoval($field_spec) { for ($i = 0; $i < 3; $i++) { $this->connection ->insert($table_name) + ->useDefaults(['serial_column']) ->fields(['test_nullable_field' => 100]) ->execute(); } // Add another row with no value for the 'test_nullable_field' column. - if ($this->connection->driver() == 'mysql') { - // MySQL uses a default of 0 for serial fields which cause rows with an - // incorrect ID to be inserted, see - // https://bugs.mysql.com/bug.php?id=89225. - $this->connection - ->insert($table_name) - ->fields(['serial_column' => NULL]) - ->execute(); - } - else { - $this->connection - ->insert($table_name) - ->useDefaults(['serial_column']) - ->execute(); - } + $this->connection + ->insert($table_name) + ->useDefaults(['serial_column']) + ->execute(); $this->schema->addField($table_name, 'test_field', $field_spec); @@ -790,18 +780,10 @@ protected function assertFieldCharacteristics($table_name, $field_name, $field_s // Check that the default value has been registered. if (isset($field_spec['default'])) { // Try inserting a row, and check the resulting value of the new column. - if ($this->connection->driver() == 'mysql') { - $id = $this->connection - ->insert($table_name) - ->fields(['serial_column' => NULL]) - ->execute(); - } - else { - $id = $this->connection - ->insert($table_name) - ->useDefaults(['serial_column']) - ->execute(); - } + $id = $this->connection + ->insert($table_name) + ->useDefaults(['serial_column']) + ->execute(); $field_value = $this->connection ->select($table_name) ->fields($table_name, [$field_name])