diff -u b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php --- b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php @@ -93,11 +93,26 @@ * {@inheritdoc} */ public function __construct(\PDO $connection, array $connection_options) { - // If the SQL mode doesn't include 'ANSI_QUOTES' (explicitly or via the - // 'ANSI' combination mode), then MySQL doesn't interpret a double quote as - // an identifier quote, in which case use the non-ANSI-standard backtick. - // @see https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sqlmode_ansi_quotes - if ($this->identifierQuotes === ['"', '"'] && strpos($connection_options['init_commands']['sql_mode'], 'ANSI') === FALSE) { + // If the SQL mode doesn't include 'ANSI_QUOTES' (explicitly or via a + // combination mode), then MySQL doesn't interpret a double quote as an + // identifier quote, in which case use the non-ANSI-standard backtick. + // + // Because we still support MySQL 5.7, check for the deprecated combination + // modes as well. + // + // @see https://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_ansi_quotes + $ansi_quotes_modes = ['ANSI_QUOTES', 'ANSI', 'DB2', 'MAXDB', 'MSSQL', 'ORACLE', 'POSTGRESQL']; + $is_ansi_quotes_mode = FALSE; + foreach ($ansi_quotes_modes as $mode) { + // None of the modes in $ansi_quotes_modes are substrings of other modes + // that are not in $ansi_quotes_modes, so a simple stripos() does not + // return false positives. + if (stripos($connection_options['init_commands']['sql_mode'], $mode) !== FALSE) { + $is_ansi_quotes_mode = TRUE; + break; + } + } + if ($this->identifierQuotes === ['"', '"'] && !$is_ansi_quotes_mode) { $this->identifierQuotes = ['`', '`']; } parent::__construct($connection, $connection_options); diff -u b/core/tests/Drupal/KernelTests/Core/Database/SqlModeTest.php b/core/tests/Drupal/KernelTests/Core/Database/SqlModeTest.php --- b/core/tests/Drupal/KernelTests/Core/Database/SqlModeTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SqlModeTest.php @@ -15,8 +15,11 @@ public function testQuotingIdentifiers() { // Use the table named an ANSI SQL reserved word with a column that is as // well. - $result = $this->connection->query('SELECT [update] FROM {select}')->fetchObject(); - $this->assertEquals('Update value 1', $result->update); + $query = $this->connection->query('SELECT [update] FROM {select}'); + $this->assertEquals('Update value 1', $query->fetchObject()->update); + if ($this->connection->databaseType() == 'mysql') { + $this->assertStringContainsString('SELECT `update` FROM `', $query->getQueryString()); + } } /**