docs
Models

Models

Define your data structures using Go structs with gx struct tags.

Embedding gormicx.Model

type Post struct {
    gormicx.Model          // provides: ID, CreatedAt, UpdatedAt, DeletedAt
    Title   string `gx:"column:title;not_null;size:500"`
    Body    string `gx:"column:body"`
    UserID  uint   `gx:"column:user_id;index"`
}

gormicx.Model provides:

FieldTypeDescription
IDuintAuto-increment primary key
CreatedAttime.TimeSet on first insert
UpdatedAttime.TimeUpdated on every save
DeletedAt*time.Timenil = active, non-nil = soft-deleted

Custom Primary Key

type Product struct {
    SKU   string  `gx:"primaryKey;column:sku"`
    Name  string  `gx:"column:name;not_null"`
    Price float64 `gx:"column:price"`
}

Custom Table Name

Implement TableName() string on your model:

func (*User) TableName() string { return "app_users" }

Struct Tag Reference (gx:"...")

Tags are semicolon-separated key or key:value pairs.

TagEffect
column:nameOverride database column name
primaryKey / pkMark as primary key
autoIncrement / autoAuto-increment primary key
not_null / notnullNOT NULL constraint
uniqueUNIQUE constraint
indexCreate an index on this column
size:NColumn size e.g. VARCHAR(255)
precision:NDecimal precision
defaultColumn has a default value
-Ignore this field entirely

Naming Strategy

By default gormicx converts Go struct names to snake_case table names (pluralised) and field names to snake_case column names.

Go structDefault table
Userusers
BlogPostblog_posts
HTTPLoghttp_logs

Customise with options:

gormicx.WithTablePrefix("app_")   // → app_users
gormicx.WithSingularTable(true)   // → user (not users)