gorm-composite-primary-key

Model composite primary keys in GORM structs with multi-column uniqueness.

Updated Apr 4, 2026
One-click install
npx skills add https://github.com/liurida/gorm-development-skill --skill gorm-composite-primary-key
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: gorm-composite-primary-key
Source: https://github.com/liurida/gorm-development-skill/tree/main/composite_primary_key
Command: npx skills add https://github.com/liurida/gorm-development-skill --skill gorm-composite-primary-key

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

When a single column cannot uniquely identify a record, composite primary keys let you enforce multi-column uniqueness at the schema level so related queries, joins, and associations remain correct and consistent.

Core Features & Use Cases

  • Define multi-column primary keys by marking each field that participates in the key so migrations and constraints reflect the combined uniqueness.
  • Integer key handling with autoincrement disabled for integer fields that are part of a composite primary key to prevent unintended value generation.
  • Associations and join tables automatically include all key columns for many-to-many relations so preloads, joins, and foreign keys remain accurate.
  • Use case: Localized product catalogs where (product_id, language_code) uniquely identify a translation record and must be queried and joined reliably.

Quick Start

Create a GORM model by marking every field that composes the unique key with the primary key tag and disable autoincrement for integer key fields when necessary.

Frequently Asked Questions about gorm-composite-primary-key

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I define a composite primary key in GORM structs?

To define a composite primary key in GORM, mark every struct field participating in the unique key with the `primaryKey` tag. This ensures schema migrations and database constraints correctly reflect the combined multi-column uniqueness.

Why does autoincrement cause issues with GORM composite primary keys?

Autoincrement can cause unintended value generation for integer fields within a composite primary key. You must explicitly disable autoincrement for integer key components to preserve correct multi-column uniqueness behavior in GORM migrations.

How do GORM associations and join tables work with composite primary keys?

GORM associations and join tables automatically include all composite primary key columns for many-to-many relations. This ensures that preloads, joins, and foreign keys remain accurate when querying records by multiple key fields.

When do I need multi-column uniqueness in a GORM database schema?

You need multi-column uniqueness in a GORM database schema when a single column cannot uniquely identify a record, such as localized product catalogs where (product_id, language_code) must uniquely identify a translation record.

Does GORM support querying records by multiple primary key fields?

Yes, GORM supports querying records by multiple primary key fields when using composite primary keys. Marking each key field as primary ensures that queries reliably retrieve records using the combined unique identifiers.