Field Helpers
The CLI generates field helpers from your model structs. Use them for typed filters, updates, ordering, and association work without hand-writing SQL strings. They plug into gorm.G[T] builders in both the default generics output and the --typed=false mode.
Supported Models & Types
- Core Go types: integers, floats,
string,bool,time.Time,[]byte - Named or custom types implementing
database/sql.Scanner/driver.Valuer - Serializer-backed fields using GORM’s
Serializerinterfaces - Associations:
has one(including polymorphic),has many(including polymorphic),belongs to,many2many
Predicates & Updates
// Predicates |
Standard API note (
--typed=false)
The default output is strictly typed. With the Standard API, you keep generics but can mix raw conditions with helpers:
generated.Query[User](db).
Where("name = ?", "jinzhu").
Where(generated.User.Age.Gt(18)).
Find(ctx)
Association Operations
Association helpers surface on generated structs as field.Struct[T] or field.Slice[T] (for example, generated.User.Pets, generated.User.Account). Combine them inside Set(...).Create(ctx) or Set(...).Update(ctx) calls.
Supported operations:
- Create — create & link a related row per parent
- CreateInBatch — batch create/link from a slice
- Update — update related rows (with optional conditions)
- Unlink — remove only the relationship (clear FK or delete join rows)
- Delete — delete related rows (m2m: deletes join rows only)
// Create a pet for each matched user |
Association semantics:
- Belongs To —
Unlinkclears the parent FK;Deleteremoves associated rows - Has One / Has Many (including polymorphic) —
Unlinkclears the child FK;Deleteremoves child rows - Many2Many —
Unlink/Deleteremove join rows only (both sides remain)
Parent operation semantics:
Create(ctx)inserts new parent rows using yourSet(...)values, then applies association operationsUpdate(ctx)updates matched parent rows, then applies association operations
Next: learn the Typed Raw SQL flow or jump back to the CLI overview.