gorm-has-one

Define GORM one-to-one relationships where the associated model holds the foreign key.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Provides clear patterns and operational guidance for establishing one-to-one relationships in GORM where the associated model holds the foreign key, reducing ambiguity about foreign key placement, eager loading, and lifecycle behavior between related models.

Core Features & Use Cases

  • Define has one associations with conventions for default foreign key naming and how the owned model stores the owner's key.
  • Configure eager loading using Preload or Joins and manage association lifecycle with Association mode operations such as Find, Append, Replace, and Clear.
  • Override foreign key and reference fields, add constraint behaviors for OnUpdate/OnDelete, and model polymorphic or self-referential one-to-one relationships.
  • Use Case: Model a User and a CreditCard where CreditCard stores UserID, preload the card when loading users, and enforce cascade or set-null behavior on deletion.

Quick Start

Create a User and CreditCard GORM model where CreditCard stores UserID as the foreign key and demonstrate Preload and Association operations for find and replace.

Frequently Asked Questions about gorm-has-one

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

FAQPage Schema
How do I define a one-to-one has one association in GORM?

To define a has one association in GORM, you establish a one-to-one relationship where the associated model holds the foreign key, relying on default naming conventions or explicit tag overrides for the foreign key and references fields.

How does eager loading work for GORM one-to-one relationships?

Eager loading for GORM one-to-one relationships works by applying the Preload or Joins patterns to fetch the associated model alongside the owner, preventing N+1 query issues when querying related data.

What is the difference between has one and belongs to in GORM model design?

In GORM model design, has one means the associated model holds the foreign key, whereas belongs to means the current model holds the foreign key. Choosing has one places the relationship key on the owned model's schema.

Can I override the foreign key and reference fields for a GORM has one relationship?

You can override the foreign key and reference fields for a GORM has one relationship by configuring structural tags. This allows customizing default column names and mapping associations to non-standard database fields.

How do I manage association CRUD operations for a GORM has one relationship?

You manage association CRUD operations for a GORM has one relationship using Association mode. This supports Find, Append, Replace, and Clear operations to handle the lifecycle of the associated model directly.

Does GORM support constraint configuration for has one associations?

GORM supports constraint configuration for has one associations by applying tag-based settings. You can enforce database-level OnUpdate and OnDelete behaviors such as cascade or set-null for the associated model.