Defining models
Map a Go struct onto a table — tag-free conventions, the Tabler interface, embedded structs, and ignored fields.
fusion-orm parses a model with Tables.Parse (or Tables.Get), reflecting
the struct into a *schema.Table: fields, primary keys, relations, and hook
presence. The parse is cached per type — repeated parses return the
identical pointer — so a query never pays for reflection again.
// Parse parses model (a struct or pointer to struct) and returns its cached
// Table. Repeated parses return the identical pointer.The model type must be a struct (or pointer to one); anything else fails
with schema.ErrInvalidModel (schema/table.go:109).
Mapping without tags
Four field-name conventions need no tag at all. They are resolved once,
during parse (schema/field.go:212-234):
| Go field name | Effect |
|---|---|
ID | Treated as the primary key, even without pk (`PrimaryKey: ... |
CreatedAt | AutoCreateTime: set at insert time. |
UpdatedAt | AutoUpdateTime: set at insert and update time. |
DeletedAt | Soft delete: every non-Unscoped query adds deleted_at IS NULL, and Delete writes the timestamp instead of removing the row. |
PrimaryKey: tag.Has("pk") || tag.Has("primarykey") || sf.Name == "ID",Name, not type
The conventions key off the field name, not the type. A field named
DeletedAt of any type becomes the soft-delete column; a field named
ID becomes the primary key even if it is a string.
Overriding the table name
By default the table name is the pluralized snake_case model name
(schema/table.go:172-174). Implement the schema.Tabler interface to
override it:
// Tabler lets a model override its table name.
type Tabler interface {
TableName() string
}type exampleUser struct {
ID int64 `fusion:"pk,autoincrement"`
Name string `fusion:"notnull"`
Profile *exampleProfile `fusion:"rel:has-one,foreignKey:UserID"`
Posts []examplePost `fusion:"rel:has-many,foreignKey:UserID"`
}
func (exampleUser) TableName() string { return "example_users" }TableName wins outright — the naming strategy never pluralizes or prefixes
an explicit value (schema/table.go:204-210).
Column naming
Without a tag, a field's column is the snake_case field name via the
configured Namer: Email → email, CreatedAt → created_at
(schema/table.go:248-255, see Naming strategy).
Two overrides exist:
- a bare tag token —
Email string \fusion:"user_email"`` — sets the column name directly; column:—Email string \fusion:"column:user_email,notnull"``.
Two fields that resolve to the same column, or two embedded structs
colliding on a Go field name, fail parse with schema.ErrAmbiguousField
(schema/table.go:260-265).
Pointer vs value fields
A pointer field (*string, *Profile) is nullable in the natural sense:
a nil pointer renders as NULL on write, and scanning NULL leaves the
pointer nil. Scanning into a nil pointer for an embedded struct allocates
it on demand (schema/field.go:117-125):
// Scan assigns src to the field, allocating nil embedded pointers as needed.Value fields do not have a nil state — a zero value writes a zero (or
NULL under nullzero, see the tag reference).
Embedded structs
Structs embedded anonymously are always flattened: their fields become
columns of the outer table. A named struct field is flattened too, when
it carries an embed tag whose value is a column-name prefix
(schema/table.go:276-289):
type tableUser struct {
auditFields
Name string
Addr address `fusion:"embed:addr_"`
}
type address struct {
City string `fusion:"city"`
}
func (tableUser) TableName() string { return "custom_users" }The columns are id, created_at, name, and addr_city — the prefix
addr_ is prepended to each child column (schema/field.go:129-134).
There is no separate embeddedPrefix key; the value of embed is the
prefix. A bare embed (empty prefix) works for anonymous-style flattening
of a named field.
Conventions survive embedding
Conventions are resolved on the flattened fields: an embedded CreatedAt
is still marked auto-create, an embedded ID is still the primary key —
the parse walks the struct tree, not the embedding boundary
(schema/table_test.go:49-51).
Ignored fields
A field tagged fusion:"-" is skipped entirely — no column, no metadata:
if tag.Has("-") {
continue
}type closureModel struct {
Bool bool
I int
I8 int8
U uint
F32 float32
F64 float64
S string
B []byte
T time.Time
M map[string]any
Slice []int
Null sql.NullString
Ptr *string
Ignored string `fusion:"-"`
}if table.Column("ignored") != nil {
t.Error("fusion:\"-\" field was parsed")
}A complete model
Pulling the pieces together — several field kinds, a tag-set column, a read-only field, and a write-only field:
type metaModel struct {
ID int64 `fusion:"pk,autoincrement"`
Email string `fusion:"user_email,notnull,index"`
Nick string `fusion:"unique,nickname"`
Blob []byte `fusion:"json"`
Custom string `fusion:"serializer:json,type:varchar(9),default:'x'"`
CreatedAt time.Time
UpdatedAt time.Time
RO string `fusion:"->"`
WO string `fusion:"<-"`
}The parsed metadata for this model (schema/field_test.go:96-108): Email
maps to column user_email, RO is not creatable or updatable, WO is
not updatable or readable, and the serializer forces Custom to the JSON
data type.