Skip to main content

Field roles

Tags combine with commas, as in ant:"default='new',immutable".

All the tags​

TagMeans
ant:"-"Not a column
ant:"column=name"Use this column name
ant:"unique"A unique constraint, used by migrations
ant:"default=expr"Leave a zero value to the column's default. Details
ant:"readonly"Filled by the database, never written
ant:"writeonly"Written, never read
ant:"immutable"Written on insert, never changed by SaveChanges
ant:"computed"Filled by Query.With, no column
ant:"version"Optimistic concurrency counter, a signed integer
ant:"token"Optimistic concurrency, with a value you manage
ant:"embedded", ant:"embedded,prefix=x_"Flatten a struct into columns

Read a value the database generates​

readonly suits generated columns and trigger-set values. ant leaves the column out of every write and reads the value back after an insert and after an update, so a column a trigger keeps, such as last_update, holds what the row does, and can serve as a token. After an update the row is read by its key in the same transaction, since SQLite's RETURNING leaves out what a trigger changes.

type Account struct {
ID int64
Email string
Slug string `ant:"readonly"` // GENERATED ALWAYS AS (lower(email))
}

account := &Account{Email: "A@X"}

err := db.Accounts.Insert(ctx, account)
// INSERT INTO "accounts" ("email") VALUES ($1) RETURNING "id", "slug"

fmt.Println(account.Slug) // "a@x"

A column declared with b.Column(&...).Generated(expr) is read-only without the tag.

Keep a value out of query results​

writeonly suits a value that must never be read back, such as a password hash. The column is written and left out of every select.

type Account struct {
ID int64
Password string `ant:"writeonly"`
}

found, err := db.Accounts.Find(ctx, id)
// SELECT "accounts"."id" FROM "accounts" WHERE "accounts"."id" = $1 LIMIT $2

fmt.Println(found.Password) // empty: never read

SaveChanges writes a changed value, and an explicit Update can set it.

Write a value once​

immutable writes the field on insert. SaveChanges ignores a later change: nothing is written and the entity is not marked modified.

type Account struct {
ID int64
CreatedBy string `ant:"immutable"`
}

account.CreatedBy = "someone else"

changed, err := session.HasChanges(ctx) // false

Explicit writes keep the same rule: a bulk Update such as db.Accounts.Where(...).Update(ctx, ant.Set(&Accounts.CreatedBy, "x")), an upsert's DoUpdate or Set, a merge's WhenMatched and UpdateMany refuse an immutable, readonly or computed field with an error naming it.

Computed​

computed has no column. Query.With fills it from an expression; without With it stays empty. A tracked entity read with With saves its other fields as usual, and never writes a computed one.

type Writer struct {
ID int64
Name string
Articles []Article
ArticleCount int64 `ant:"computed"`
}

type Article struct {
ID int64
WriterID int64
}

var Writers = ant.Fields[Writer]()

writers, err := db.Writers.
With(&Writers.ArticleCount, ant.CountRelated(&Writers.Articles)).
All(ctx)
// SELECT "writers"."id", "writers"."name",
// (SELECT COUNT(*) FROM "articles" AS "ant_related" WHERE "ant_related"."writer_id" = "writers"."id") AS "article_count"
// FROM "writers"

More in relation aggregates.