Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Util/ColumnParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ColumnParser
public function parseFields(array $arguments): array
{
$fields = [];
$primaryKeys = [];
$arguments = $this->validArguments($arguments);
foreach ($arguments as $field) {
preg_match($this->regexpParseColumn, $field, $matches);
Expand Down Expand Up @@ -90,11 +91,23 @@ public function parseFields(array $arguments): array
}
}

if ($isPrimaryKey) {
$primaryKeys[] = $field;
}

if ($isPrimaryKey && $type === 'integer') {
$fields[$field]['options']['autoIncrement'] = true;
}
}

// A composite primary key (e.g. a join table) must not auto-increment.
// Only a single integer primary key column gets auto-increment.
if (count($primaryKeys) > 1) {
foreach ($primaryKeys as $field) {
unset($fields[$field]['options']['autoIncrement']);
}
}

return $fields;
}

Expand Down
34 changes: 34 additions & 0 deletions tests/TestCase/Util/ColumnParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,40 @@ public function testParseFields(): void
$this->assertEquals($expected, $actual);
}

/**
* A composite primary key (e.g. a join table) must not auto-increment its
* integer columns, otherwise MySQL rejects the table with
* "there can be only one auto column".
*
* @return void
*/
public function testParseFieldsCompositePrimaryKey(): void
{
$expected = [
'article_id' => [
'columnType' => 'integer',
'options' => [
'null' => false,
'default' => null,
'limit' => 11,
],
],
'tag_id' => [
'columnType' => 'integer',
'options' => [
'null' => false,
'default' => null,
'limit' => 11,
],
],
];
$actual = $this->columnParser->parseFields([
'article_id:integer:primary',
'tag_id:integer:primary',
]);
$this->assertEquals($expected, $actual);
}

public function testParseIndexes(): void
{
$this->assertEquals(['UNIQUE_ID' => [
Expand Down
Loading