Skip to content

fix: don't reduce a composite primary key to its autoIncrement column - #244

Open
h2zi wants to merge 1 commit into
go-gorm:masterfrom
h2zi:fix-composite-pk-autoincrement
Open

fix: don't reduce a composite primary key to its autoIncrement column#244
h2zi wants to merge 1 commit into
go-gorm:masterfrom
h2zi:fix-composite-pk-autoincrement

Conversation

@h2zi

@h2zi h2zi commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Symptom

type Model struct {
    A int    `gorm:"primaryKey;autoIncrement"`
    B string `gorm:"primaryKey"`
}

AutoMigrate succeeds but creates (`a` integer PRIMARY KEY AUTOINCREMENT, `b` text) — B's primary-key declaration is silently dropped (related reports: #134, #126, glebarez#148).

Cause

DataTypeOf emits integer PRIMARY KEY AUTOINCREMENT for any auto-increment int field. When the rendered field type already contains PRIMARY KEY, GORM skips the table-level PRIMARY 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 — including autoIncrement without an explicit primaryKey tag — is unchanged, verified by pragma_table_info in 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.

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.
Copilot AI review requested due to automatic review settings July 31, 2026 09:34

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.

🟢 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 dataTypeOf from emitting integer PRIMARY KEY AUTOINCREMENT when the field participates in a multi-column primary key.
  • Add a regression test asserting AutoMigrate preserves 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.

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