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 database constraints.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
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:
- BaseModel embedding (always first)
- Foreign keys (e.g., ProjectID, SymbolID)
- Required business fields (UID, Label, ClassName, etc.)
- Optional fields
- 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
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-01-26
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!