Back to Skills

creating-gorm-entity

majiayu000
Updated Today
58
9
58
View on GitHub
Metaaidata

About

This skill generates GORM entity structs for go-kratos microservices, embedding a BaseModel for timestamps and soft deletes. It properly configures field tags, relationships (one-to-one/one-to-many), and indexing strategies. Use it when defining database schemas, creating new entities, or setting up table relationships and constraints.

Quick Install

Claude Code

Recommended
Plugin CommandRecommended
/plugin add https://github.com/majiayu000/claude-skill-registry
Git CloneAlternative
git clone https://github.com/majiayu000/claude-skill-registry.git ~/.claude/skills/creating-gorm-entity

Copy and paste this command in Claude Code to install this skill

Documentation

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

GitHub Repository

majiayu000/claude-skill-registry
Path: skills/creating-gorm-entity

Related Skills

content-collections

Meta

This skill provides a production-tested setup for Content Collections, a TypeScript-first tool that transforms Markdown/MDX files into type-safe data collections with Zod validation. Use it when building blogs, documentation sites, or content-heavy Vite + React applications to ensure type safety and automatic content validation. It covers everything from Vite plugin configuration and MDX compilation to deployment optimization and schema validation.

View skill

sglang

Meta

SGLang is a high-performance LLM serving framework that specializes in fast, structured generation for JSON, regex, and agentic workflows using its RadixAttention prefix caching. It delivers significantly faster inference, especially for tasks with repeated prefixes, making it ideal for complex, structured outputs and multi-turn conversations. Choose SGLang over alternatives like vLLM when you need constrained decoding or are building applications with extensive prefix sharing.

View skill

evaluating-llms-harness

Testing

This Claude Skill runs the lm-evaluation-harness to benchmark LLMs across 60+ standardized academic tasks like MMLU and GSM8K. It's designed for developers to compare model quality, track training progress, or report academic results. The tool supports various backends including HuggingFace and vLLM models.

View skill

langchain

Meta

LangChain is a framework for building LLM applications using agents, chains, and RAG pipelines. It supports multiple LLM providers, offers 500+ integrations, and includes features like tool calling and memory management. Use it for rapid prototyping and deploying production systems like chatbots, autonomous agents, and question-answering services.

View skill