fix: don't reduce a composite primary key to its autoIncrement column - #244
Open
h2zi wants to merge 1 commit into
Open
fix: don't reduce a composite primary key to its autoIncrement column#244h2zi wants to merge 1 commit into
h2zi wants to merge 1 commit into
Conversation
DataTypeOf emitted "integer PRIMARY KEY AUTOINCREMENT" for any auto-increment int field. With a composite primary key GORM then skips the table-level PRIMARY KEY clause (the field type already contains PRIMARY KEY), so the other key columns silently lost their primary-key status. AUTOINCREMENT only applies to a single-column INTEGER PRIMARY KEY anyway, so it is dropped when the field is part of a multi-column key and the table-level PRIMARY KEY (a, b) is emitted instead. The single-key behavior — including autoIncrement without an explicit primaryKey tag — is unchanged.
There was a problem hiding this comment.
🟢 Ready to approve
The change is narrowly scoped to SQLite type rendering for composite keys and is covered by a targeted regression test that reproduces the reported silent primary-key drop.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Pull request overview
Fixes SQLite DDL generation for models that declare a composite primary key where one column is tagged autoIncrement, ensuring the composite key is preserved instead of being silently reduced to a single-column INTEGER PRIMARY KEY AUTOINCREMENT.
Changes:
- Prevent
dataTypeOffrom emittinginteger PRIMARY KEY AUTOINCREMENTwhen the field participates in a multi-column primary key. - Add a regression test asserting
AutoMigratepreserves both primary-key columns for a composite key model.
File summaries
| File | Description |
|---|---|
| sqlite.go | Avoids emitting INTEGER PRIMARY KEY AUTOINCREMENT for auto-increment fields in composite primary keys so table-level PRIMARY KEY(a,b) is not suppressed. |
| migrator_test.go | Adds a regression test that verifies composite primary keys retain all key columns after AutoMigrate. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
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
AutoMigratesucceeds but creates(`a` integer PRIMARY KEY AUTOINCREMENT, `b` text)— B's primary-key declaration is silently dropped (related reports: #134, #126, glebarez#148).Cause
DataTypeOfemitsinteger PRIMARY KEY AUTOINCREMENTfor any auto-increment int field. When the rendered field type already containsPRIMARY KEY, GORM skips the table-levelPRIMARY KEY (a, b)clause, so the composite key degrades to a single column.Fix
AUTOINCREMENT only applies to a single-column INTEGER PRIMARY KEY in SQLite, so when the field is part of a multi-column key the clause is dropped and the table-level
PRIMARY KEY (a, b)takes effect. Single-key behavior — includingautoIncrementwithout an explicitprimaryKeytag — is unchanged, verified bypragma_table_infoin the test.🤖 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.