Skip to content

Do not auto-increment composite primary key columns in bake migration#1095

Merged
dereuromark merged 2 commits into
5.xfrom
composite-pk-no-autoincrement
Jul 3, 2026
Merged

Do not auto-increment composite primary key columns in bake migration#1095
dereuromark merged 2 commits into
5.xfrom
composite-pk-no-autoincrement

Conversation

@dereuromark

@dereuromark dereuromark commented Jun 30, 2026

Copy link
Copy Markdown
Member

Problem

Baking a join-table migration with a composite integer primary key, as the docs recommend:

bin/cake bake migration CreateArticlesTags article_id:integer:primary tag_id:integer:primary created modified

generates 'autoIncrement' => true on both primary key columns. MySQL then rejects the table:

SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition;
there can be only one auto column and it must be defined as a key

Reported in cakephp/cakephp#19524.

Fix

By convention only a single integer primary key auto-increments; a composite primary key (the normal shape for join tables) must not. ColumnParser::parseFields() now only emits autoIncrement when there is exactly one primary key column. Single-PK behavior is unchanged.

Added testParseFieldsCompositePrimaryKey covering the regression.

Resolves #1096

Baking a migration with multiple integer primary key columns, e.g. a join
table:

    bin/cake bake migration CreateArticlesTags article_id:integer:primary tag_id:integer:primary

generated `autoIncrement => true` on every integer primary key column. MySQL
then rejects the table:

    SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table
    definition; there can be only one auto column and it must be defined as a key

By convention only a single integer primary key auto-increments; a composite
primary key (typical for join tables) must not. ColumnParser now only emits
autoIncrement when there is exactly one primary key column.
@dereuromark dereuromark requested review from ADmad and markstory July 3, 2026 07:45
@dereuromark dereuromark merged commit 46bab41 into 5.x Jul 3, 2026
14 checks passed
@dereuromark dereuromark deleted the composite-pk-no-autoincrement branch July 3, 2026 07:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible issue in the Bake migration documentation

2 participants