Skip to content

fix(ddlmod): recognize the ? placeholder in constraint clauses - #241

Open
h2zi wants to merge 1 commit into
go-gorm:masterfrom
h2zi:fix-constraint-placeholder
Open

fix(ddlmod): recognize the ? placeholder in constraint clauses#241
h2zi wants to merge 1 commit into
go-gorm:masterfrom
h2zi:fix-constraint-placeholder

Conversation

@h2zi

@h2zi h2zi commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Symptom

AutoMigrate on an existing table that is missing a foreign key declared in the model fails with:

table constraint_children__temp has no column named CONSTRAINT

Cause

CreateConstraint appends the constraint to the DDL field list in the form produced by constraint.Build(): CONSTRAINT ? FOREIGN KEY .... The constraintRegexp used by getColumns to filter out constraint clauses requires a backquoted constraint name, so the placeholder clause isn't recognized — the literal token CONSTRAINT is extracted as a column name and lands in the rebuild's INSERT INTO ... SELECT.

Fix

Accept a quoted or unquoted name as well as the ? placeholder, matching the more tolerant compileConstraintRegexp already used by addConstraint/removeConstraint. The now-unused sqliteColumnQuote is removed.

Covered by an end-to-end test: create the parent, create the child table without the FK, then AutoMigrate the child model — previously failing, now the constraint is added and data survives the rebuild.

🤖 Generated with Claude Code


Merge order (this batch: #241#242#243#244#245#246, plus #234 from the previous batch): all seven are functionally independent and merge cleanly in any order. Several append tests to the same test files, so whichever merges later may show a trivial append-only conflict in migrator_test.go/ddlmod_test.go — I'll rebase the remaining ones promptly after each merge, as before.

CreateConstraint appends the constraint to the DDL field list in the
form produced by constraint.Build(): `CONSTRAINT ? FOREIGN KEY ...`.
constraintRegexp required a backquoted constraint name, so getColumns
did not filter the clause out and extracted the literal `CONSTRAINT` as
a column name; the rebuild's data copy then failed with

    table x__temp has no column named CONSTRAINT

which breaks AutoMigrate whenever an existing table is missing a
foreign key declared in the model. The regexp now accepts a quoted or
unquoted name as well as the ? placeholder, matching the more tolerant
compileConstraintRegexp used elsewhere.
Copilot AI review requested due to automatic review settings July 31, 2026 09:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes SQLite AutoMigrate table-rebuild failures when GORM emits constraint clauses using the CONSTRAINT ? ... placeholder format, ensuring those clauses are not mis-parsed as column definitions during rebuild/copy.

Changes:

  • Update DDL parsing to recognize CONSTRAINT ? ... (and more broadly tolerate quoted/unquoted constraint names) when filtering constraint clauses.
  • Remove the now-unused sqliteColumnQuote constant.
  • Add an end-to-end regression test that reproduces the failure and verifies data survival + FK creation after AutoMigrate.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
ddlmod.go Broadens constraintRegexp so getColumns/DDL parsing skips CONSTRAINT ? ... clauses instead of treating CONSTRAINT as a column name.
migrator_test.go Adds a regression test covering AutoMigrate of an existing table missing an FK, ensuring rebuild succeeds and data remains.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants