fix(ddlmod): recognize the ? placeholder in constraint clauses - #241
Open
h2zi wants to merge 1 commit into
Open
Conversation
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.
There was a problem hiding this comment.
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
sqliteColumnQuoteconstant. - 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.
This was referenced Jul 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Symptom
AutoMigrateon an existing table that is missing a foreign key declared in the model fails with:Cause
CreateConstraintappends the constraint to the DDL field list in the form produced byconstraint.Build():CONSTRAINT ? FOREIGN KEY .... TheconstraintRegexpused bygetColumnsto filter out constraint clauses requires a backquoted constraint name, so the placeholder clause isn't recognized — the literal tokenCONSTRAINTis extracted as a column name and lands in the rebuild'sINSERT INTO ... SELECT.Fix
Accept a quoted or unquoted name as well as the
?placeholder, matching the more tolerantcompileConstraintRegexpalready used byaddConstraint/removeConstraint. The now-unusedsqliteColumnQuoteis removed.Covered by an end-to-end test: create the parent, create the child table without the FK, then
AutoMigratethe 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.