go-gorm-model
Original:🇺🇸 English
Translated
Generate Go GORM models following Pingo modular architecture conventions. Use when creating or updating persistence models in internal/modules/<module>/model/, including table mapping, nullable SQL types, timestamps, and relation fields for identity and monitor modules.
2installs
Added on
NPX Install
npx skill4agent add cristiano-pacheco/ai-tools go-gorm-modelTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Go GORM Model
Generate GORM persistence models in .
internal/modules/<module>/model/Pattern
Model files must follow this location and naming:
- Path:
internal/modules/<module>/model/<entity>_model.go - Package:
model - Struct name:
<Entity>Model - TableName method:
func (*<Entity>Model) TableName() string { return "<table_name>" }
File Structure
Use this order:
package model- Imports (,
timeonly when needed)database/sql - Struct definition
- method
TableName()
Base Template
go
package model
import (
"database/sql"
"time"
)
type EntityModel struct {
ID uint64 `gorm:"primarykey"`
Name string `gorm:"column:name"`
Meta sql.NullString `gorm:"column:meta"`
CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"`
}
func (*EntityModel) TableName() string {
return "entities"
}If a field name matches GORM defaults and project style keeps it untagged, omit explicit .
gorm:"column:..."Conventions from Current Codebase
IDs and Primary Keys
- Use for numeric IDs.
uint64 - Set primary key as:
ID uint64 `gorm:"primarykey"`
Time fields
- Use for required timestamps.
time.Time - Common fields:
CreatedAt time.TimeUpdatedAt time.Time
- Use explicit column tags for snake_case DB columns when needed.
Nullable database fields
Use nullable types when DB column can be null:
database/sqlsql.NullStringsql.NullInt32sql.NullBoolsql.NullTime
Examples in this repo:
Nonce sql.NullStringStatusCode sql.NullInt32ResponseTimeMs sql.NullInt32
Column tags
Use explicit tags when any of these apply:
- DB column differs from Go field naming
- You want consistency with existing model files
- Composite or relation keys need explicit mapping
Pattern:
Field string `gorm:"column:field_name"`
Table name mapping
Always implement and return the exact SQL table name.
TableName()Examples:
authorization_codesexternal_accountshttp_monitor_checkscontacts
Generation Steps
- Identify module and entity.
- Open migration/schema and confirm table + columns.
- Create or update .
internal/modules/<module>/model/<entity>_model.go - Define struct fields with correct Go and nullable SQL types.
- Add tags (
gorm,primarykey) where needed.column:... - Add with exact table name.
TableName() - Ensure naming aligns with repository/usecase expectations.
- Run and
make test.make lint
Type Mapping Guide
Use these defaults unless migration requires otherwise:
- ->
BIGINT/UNSIGNED BIGINTuint64 - ->
VARCHAR/TEXTstring - ->
BOOLEANbool - ->
TIMESTAMP/DATETIMEtime.Time - nullable string/int/time -> /
sql.NullString/sql.NullInt32sql.NullTime - binary hash columns ->
[]byte
Example: Identity-style Model
go
type AuthorizationCodeModel struct {
ID uint64 `gorm:"primarykey"`
CodeHash []byte `gorm:"column:code_hash"`
UserID uint64 `gorm:"column:user_id"`
ClientID string `gorm:"column:client_id"`
RedirectURI string `gorm:"column:redirect_uri"`
Scope string `gorm:"column:scope"`
CodeChallenge string `gorm:"column:code_challenge"`
CodeChallengeMethod string `gorm:"column:code_challenge_method;default:S256"`
Nonce sql.NullString `gorm:"column:nonce"`
ExpiresAt time.Time `gorm:"column:expires_at"`
CreatedAt time.Time `gorm:"column:created_at"`
}Example: Monitor-style Model
go
type HTTPMonitorCheckModel struct {
ID uint64 `gorm:"primarykey"`
HTTPMonitorID uint64 `gorm:"column:http_monitor_id"`
CheckedAt time.Time `gorm:"column:checked_at"`
ResponseTimeMs sql.NullInt32 `gorm:"column:response_time_ms"`
StatusCode sql.NullInt32 `gorm:"column:status_code"`
Success bool `gorm:"column:success"`
ErrorMessage sql.NullString `gorm:"column:error_message"`
}Critical Rules
- Models are persistence only; business logic belongs in usecases.
- Do not expose GORM models directly in HTTP DTO responses.
- Keep field names and types aligned with SQL migrations.
- Do not change existing column/table names without migration updates.
- Use module-local model package only ().
internal/modules/<module>/model - Never use tags on GORM models.
json
Checklist
- File created in
internal/modules/<module>/model/ - Struct named
<Entity>Model - with
ID uint64gorm:"primarykey" - Nullable columns mapped with nullable types
database/sql - Timestamp fields typed correctly
- added with exact table name
TableName() - Tags and naming consistent with existing module style
- and
make testexecutedmake lint