* @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());
];
}, $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) {
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) {