pages/cli/field_helpers.md
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.
string, bool, time.Time, []bytedatabase/sql.Scanner / driver.ValuerSerializer interfaceshas one (including polymorphic), has many (including polymorphic), belongs to, many2many// Predicates
generated.User.ID.Eq(1) // id = 1
generated.User.Name.Like("%jinzhu%") // name LIKE '%jinzhu%'
generated.User.Age.Between(18, 65) // age BETWEEN 18 AND 65
generated.User.Score.IsNull() // score IS NULL (e.g., sql.NullInt64)
// Updates (supports expressions and zero-values)
gorm.G[User](db).
Where(generated.User.Name.Eq("alice")).
Set(
generated.User.Name.Set("jinzhu"),
generated.User.IsAdult.Set(false),
generated.User.Score.Set(sql.NullInt64{}),
generated.User.Count.Incr(1),
generated.User.Age.SetExpr(clause.Expr{
SQL: "GREATEST(?, ?)",
Vars: []any{clause.Column{Name: "age"}, 18},
}),
).
Update(ctx)
// Create with Set(...)
gorm.G[User](db).
Set(
generated.User.Name.Set("alice"),
generated.User.Age.Set(0),
generated.User.Status.Set("active"),
).
Create(ctx)
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:gogenerated.Query[User](db). Where("name = ?", "jinzhu"). Where(generated.User.Age.Gt(18)). Find(ctx)
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 a pet for each matched user
gorm.G[User](db).
Where(generated.User.ID.Eq(1)).
Set(generated.User.Pets.Create(generated.Pet.Name.Set("fido"))).
Update(ctx)
// Filter on the child before acting
gorm.G[User](db).
Where(generated.User.ID.Eq(1)).
Set(generated.User.Pets.Where(generated.Pet.Name.Eq("old")).Delete()).
Update(ctx)
// Batch link two pets to an existing user
gorm.G[User](db).
Where(generated.User.ID.Eq(1)).
Set(generated.User.Pets.CreateInBatch([]models.Pet{{Name: "rex"}, {Name: "spot"}})).
Update(ctx)
Association semantics:
Unlink clears the parent FK; Delete removes associated rowsUnlink clears the child FK; Delete removes child rowsUnlink/Delete remove join rows only (both sides remain)Parent operation semantics:
Create(ctx) inserts new parent rows using your Set(...) values, then applies association operationsUpdate(ctx) updates matched parent rows, then applies association operationsNext: learn the Typed Raw SQL flow or jump back to the CLI overview.