gorm-belongs-to

Establishes one-to-one ownership in Go using GORM foreignKey and references tags.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

The belongs-to pattern enables a child model to reference a single parent via a foreign key, establishing a clear ownership relationship in the data model.

Core Features & Use Cases

  • One-to-One Ownership: Place the foreign key on the child model and define the belongs-to association to the parent.
  • Flexible References: Use foreignKey and references tags to customize keys and referenced fields.
  • Eager Loading: Load the related parent efficiently using Preload or Joins.

Quick Start

Declare a foreign key on the child model and add a belongs-to association to the parent.

Frequently Asked Questions about gorm-belongs-to

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

FAQPage Schema
How do I model a one-to-one ownership relationship in GORM with a foreign key on the child model?

Model a one-to-one ownership relationship in GORM by declaring a foreign key on the child model and using the belongs-to association to reference the parent. Use foreignKey and references tags to configure the mapping and support eager loading.

What is the belongs-to association pattern used for in GORM data models?

The belongs-to association pattern in GORM establishes clear one-to-one ownership by allowing a child model to reference a single parent entity. It is typically applied to relationships like a User belonging to a Company or an Employee having a Manager.

How do I customize the foreign key and references fields for a GORM belongs-to relationship?

Customize the foreign key and references fields for a GORM belongs-to relationship by applying the foreignKey and references struct tags. These tags explicitly map the child model's foreign key column to the specific referenced field on the parent model.

Does GORM support eager loading for belongs-to one-to-one associations?

GORM supports eager loading for belongs-to one-to-one associations using Preload or Joins. This efficiently loads the related parent model alongside the child model, preventing N+1 query issues when retrieving ownership data.

When should I use a belongs-to association instead of a has-one relationship in GORM?

Use a belongs-to association in GORM when the foreign key resides on the declaring child model that references a parent. This approach establishes clear ownership from the child's perspective, whereas has-one places the foreign key on the referenced model.

Can I apply database constraints when configuring a belongs-to relationship in GORM?

You can apply database constraints when configuring a belongs-to relationship in GORM by using the constraint tag. This ensures referential integrity is enforced at the database level for your one-to-one ownership models.