Skip to content
creating-gorm-entity logo

GORM Entity Creation Skill

creating-gorm-entity

Creates GORM entity structs for go-kratos microservices with BaseModel embedding, proper field tags, relationships (one-to-one, one-to-many), indexing strategies, and soft delete support. Use when defining database schema, creating new entities, setting up table relationships, or configuring data...

SKILL.md

Full skill instructions

GORM Entity Creation Skill

Purpose

Generate GORM entity structs that follow the project's Clean Architecture patterns, with proper field tags, relationships, indexing strategies, and soft delete support.

Essential Patterns

1. BaseModel Pattern

All entities MUST embed BaseModel which provides:

  • ID uint64 - Primary key with auto-increment
  • CreatedAt time.Time - Automatic timestamp
  • UpdatedAt time.Time - Automatic timestamp
  • DeletedAt gorm.DeletedAt - Soft delete support with index
type BaseModel struct {
    ID        uint64 `gorm:"primaryKey;autoIncrement"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt gorm.DeletedAt `gorm:"index"`
}

2. Field Ordering Rules

Fields MUST follow this exact order:

  1. BaseModel embedding (always first)
  2. Foreign keys (e.g., ProjectID, SymbolID)
  3. Required business fields (UID, Label, ClassName, etc.)
  4. Optional fields
  5. Relationship fields (pointer types with gorm tags)

Example:

type Symbol struct {
    BaseModel                                    // 1. Base embedding
    ProjectID       uint64      `gorm:"..."     // 2. Foreign key
    UID             string      `gorm:"..."     // 3. Required fields
    Label           string      `gorm:"..."
    ClassName       string      `gorm:"..."
    ComponentTarget string      `gorm:"..."
    Version         uint32      `gorm:"..."
    SymbolData      *SymbolData `gorm:"..."     // 4. Relationship
}

3. GORM Tag Structure

Tags MUST follow this pattern: `gorm:"constraint1;constraint2;..." json:"field_name"`

Common constraints:

  • `not null` - Required field
  • `size:255` - String length limit
  • `uniqueIndex:idx_name,priority:N` - Unique composite index
  • `index:idx_name,priority:N` - Non-unique composite index
  • `foreignKey:FieldName` - FK relationship
  • `references:ID` - FK reference
  • `constraint:OnDelete:CASCADE` - FK cascade delete

Critical indexing patterns:

// Composite unique index (project_id + uid)
ProjectID uint64 \`gorm:"not null;uniqueIndex:idx_project_uid,priority:1" json:"project_id"\`
UID       string \`gorm:"not null;size:255;uniqueIndex:idx_project_uid,priority:2" json:"uid"\`

// Multiple indexes on same field
ProjectID uint64 \`gorm:"not null;uniqueIndex:idx_project_uid,priority:1;index:idx_project_id,priority:1;index:idx_symbols_project_deleted_at,priority:1" json:"project_id"\`

4. Relationship Patterns

One-to-One with Cascade Delete:

type Symbol struct {
    BaseModel
    // ... other fields ...
    SymbolData *SymbolData \`gorm:"foreignKey:SymbolID;references:ID;constraint:OnDelete:CASCADE" json:"symbol_data,omitempty"\`
}

type SymbolData struct {
    BaseModel
    SymbolID uint64  \`gorm:"not null;uniqueIndex" json:"symbol_id"\`
    Data     *[]byte \`gorm:"not null;type:longblob" json:"data"\`
}

5. Table Naming

MUST provide explicit table name method:

func (Symbol) TableName() string {
    return "symbols"  // plural, snake_case
}

Validation Checklist

When creating a GORM entity, verify:

  • BaseModel is embedded as first field
  • All foreign keys come before business fields
  • Required fields have `not null` tag
  • String fields have `size:N` constraint
  • Unique combinations use `uniqueIndex` with priorities
  • Frequently queried fields are indexed
  • Foreign keys have proper relationship tags
  • Cascade deletes are configured where needed
  • TableName() method returns plural snake_case
  • JSON tags use snake_case naming
  • Pointer types used for optional relationships
  • `omitempty` added to optional json fields

Anti-Patterns

❌ DON'T:

  • Use `gorm.Model` (use `BaseModel` instead)
  • Forget to index foreign keys
  • Use value types for optional relationships
  • Mix camelCase and snake_case in json tags
  • Omit `TableName()` method
  • Put relationship fields before business fields

✅ DO:

  • Always embed `BaseModel`
  • Index all foreign keys
  • Use pointer types for optional relationships
  • Use snake_case for all json tags
  • Provide explicit `TableName()` method
  • Follow field ordering rules

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View