Skip to content

fix(schema): stop double-emitting foreign keys in Schema::create() - #65

Merged
benkhalife merged 1 commit into
mainfrom
fix/schema-create-duplicate-foreign-keys
Aug 4, 2026
Merged

fix(schema): stop double-emitting foreign keys in Schema::create()#65
benkhalife merged 1 commit into
mainfrom
fix/schema-create-duplicate-foreign-keys

Conversation

@benkhalife

Copy link
Copy Markdown
Member

Schema::create() compiled every $table->foreign() twice: once inlined as a column-level CONSTRAINT by compileCreate(), and again as a separate ALTER TABLE ... ADD CONSTRAINT via compileForeignKeys() — no grammar suppresses the second pass. MySQL rejects the duplicate constraint (errno 121) and SQLite rejects the statement outright (ALTER TABLE ADD CONSTRAINT isn't valid SQLite DDL), so any table defined with foreign() could never be created via Schema::create() on either driver.

Fix: drop the redundant compileForeignKeys() pass from Schema::create() — the constraints are already present from compileCreate(). This does not affect Schema::table(), which still needs compileForeignKeys() to add constraints to already-existing tables.

Adds tests/Integration/SchemaForeignKeyTest.php, reproduced and verified fixed against real MySQL (errno 121) and SQLite (syntax error) connections; full unit + integration suites pass on both drivers.

Schema::create() compiled every $table->foreign() twice: once inlined
as a column-level CONSTRAINT by compileCreate(), and again as a
separate ALTER TABLE ... ADD CONSTRAINT via compileForeignKeys() —
no grammar suppresses the second pass. MySQL rejects the duplicate
constraint (errno 121) and SQLite rejects the statement outright
(ALTER TABLE ADD CONSTRAINT isn't valid SQLite DDL), so any table
defined with foreign() could never be created via Schema::create()
on either driver.

Fix: drop the redundant compileForeignKeys() pass from Schema::create()
— the constraints are already present from compileCreate(). This does
not affect Schema::table(), which still needs compileForeignKeys() to
add constraints to already-existing tables.

Adds tests/Integration/SchemaForeignKeyTest.php, reproduced and
verified fixed against real MySQL (errno 121) and SQLite (syntax
error) connections; full unit + integration suites pass on both
drivers.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@benkhalife
benkhalife merged commit 6ce5fb3 into main Aug 4, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant