From: Alexander Ebert Date: Mon, 9 Dec 2019 00:00:23 +0000 (+0100) Subject: MySQL requires auto columns to be the primary key X-Git-Tag: 5.2.0_RC_1~3 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=36357b9c72bb306e097230005f900d65069ac4d6;p=GitHub%2FWoltLab%2FWCF.git MySQL requires auto columns to be the primary key Adding an auto column to an existing table wasn't possible, because the primary key would be added in a secondary step. However, MySQL does not permit this to be split into two steps, requiring us to define the key when adding the column. --- diff --git a/com.woltlab.wcf/files_pre.tar b/com.woltlab.wcf/files_pre.tar index acdf4ad307..513c0bc645 100644 Binary files a/com.woltlab.wcf/files_pre.tar and b/com.woltlab.wcf/files_pre.tar differ diff --git a/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php b/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php index b0897945ce..261de6ae47 100644 --- a/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php +++ b/wcfsetup/install/files/lib/system/database/table/DatabaseTableChangeProcessor.class.php @@ -582,9 +582,15 @@ class DatabaseTableChangeProcessor { * @param DatabaseTable $table */ protected function createTable(DatabaseTable $table) { - $columnData = array_map(function(IDatabaseTableColumn $column) { + $hasPrimaryKey = false; + $columnData = array_map(function(IDatabaseTableColumn $column) use (&$hasPrimaryKey) { + $data = $column->getData(); + if (isset($data['key']) && $data['key'] === 'PRIMARY') { + $hasPrimaryKey = true; + } + return [ - 'data' => $column->getData(), + 'data' => $data, 'name' => $column->getName() ]; }, $table->getColumns()); @@ -595,6 +601,13 @@ class DatabaseTableChangeProcessor { ]; }, $table->getIndices()); + // Auto columns are implicitly defined as the primary key by MySQL. + if ($hasPrimaryKey) { + $indexData = array_filter($indexData, function($key) { + return $key !== 'PRIMARY'; + }, ARRAY_FILTER_USE_KEY); + } + $this->dbEditor->createTable($table->getName(), $columnData, $indexData); foreach ($table->getForeignKeys() as $foreignKey) { diff --git a/wcfsetup/install/files/lib/system/database/table/column/AbstractDatabaseTableColumn.class.php b/wcfsetup/install/files/lib/system/database/table/column/AbstractDatabaseTableColumn.class.php index 3ca213c3cf..3c84ac5e7f 100644 --- a/wcfsetup/install/files/lib/system/database/table/column/AbstractDatabaseTableColumn.class.php +++ b/wcfsetup/install/files/lib/system/database/table/column/AbstractDatabaseTableColumn.class.php @@ -61,6 +61,12 @@ abstract class AbstractDatabaseTableColumn implements IDatabaseTableColumn { if ($this instanceof IAutoIncrementDatabaseTableColumn) { $data['autoIncrement'] = $this->isAutoIncremented() ? 1 : 0; + + // MySQL requires that there is only a single auto column per table *AND* + // that this column is defined as the primary key. + if ($data['autoIncrement'] === 1) { + $data['key'] = 'PRIMARY'; + } } if ($this instanceof IDecimalsDatabaseTableColumn && $this->getDecimals() !== null) {