gorm-polymorphism

Define polymorphic has-one and has-many associations in GORM.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Many applications need a child model to belong to different parent models, but traditional foreign keys require separate columns per parent type, causing schema bloat and inflexible queries. This skill provides a unified way to model such relationships in GORM.

Core Features & Use Cases

  • Unified Owner Columns: Stores both owner ID and owner type in a single table, reducing schema complexity.
  • Has‑One and Has‑Many Support: Works for singular and collection associations, e.g., a comment belonging to posts or videos.
  • Custom Column Names: Allows overriding default column names and type values for legacy schemas.
  • Eager Loading & Association Mode: Preload polymorphic data and manage relationships with GORM’s association API.
  • Practical Scenarios: Comment systems, taggable items, address books, and activity feeds where multiple entity types share a common child model.

Quick Start

Ask the skill to configure a polymorphic has‑many relationship between posts and comments.

Frequently Asked Questions about gorm-polymorphism

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

FAQPage Schema
How do I set up a polymorphic association in GORM for a child model that belongs to multiple parent types?

To set up a polymorphic association in GORM, you define a has-one or has-many relationship using a unified owner ID and owner type column. This allows a child model, such as a comment, to dynamically belong to different parent models like posts or videos.

What is the best way to store comments for different parent models in a single Go database table?

The best way to store comments for different parent models in Go is using polymorphic associations. This stores both the owner ID and owner type in a single table, reducing schema complexity by avoiding separate foreign key columns for each parent type.

Can I use custom column names for polymorphic fields in GORM to integrate with legacy schemas?

Yes, you can use custom column names for polymorphic fields in GORM. The implementation allows overriding default column names and type values to seamlessly map polymorphic associations onto existing legacy database schemas.

Does GORM support eager loading for polymorphic has-many relationships?

Yes, GORM supports eager loading for polymorphic has-many relationships. You can preload polymorphic data and manage the relationships using GORM's association mode API without relying on database foreign key constraints.

When should I use a polymorphic association instead of standard foreign keys in a Go application?

You should use a polymorphic association instead of standard foreign keys when a child model needs to belong to multiple different parent types. This approach prevents schema bloat by using unified owner columns rather than adding separate foreign keys for every parent model.

Why are my polymorphic associations not working without database foreign key constraints in GORM?

Polymorphic associations in GORM operate without relying on database foreign key constraints because the relationship is managed through application-level owner ID and type columns. Operations are handled directly via GORM's association API rather than database-level enforcement.