Skip to main content

Configure

Configure is an optional method that overrides conventions in Go, using field pointers for column names so the compiler checks them. It runs once, when a context containing the model opens.

Add a Configure method​

type Plan struct {
Slug string
Title string
}

func (Plan) Configure(b *ant.ModelBuilder[Plan]) {
b.Table("pricing_plans")
b.Key(&Plans.Slug)
}

var Plans = ant.Fields[Plan]()
Mistakes stop Open

An empty table name, a relation used as a key, or a field pointer from another model's probe fails when the database opens.

Columns​

b.Column describes the database column, and migrations and EnsureCreated use it to build the table. Generated also makes the field read-only at run time, and Sensitive keeps its values out of logs.

func (Article) Configure(b *ant.ModelBuilder[Article]) {
b.Column(&Articles.Title).Length(200) // varchar(200)
b.Column(&Articles.Price).Precision(10, 2) // numeric(10,2)
b.Column(&Articles.Code).Fixed(4) // char(4)
b.Column(&Articles.Status).Type("varchar(20)") // any SQL type
b.Column(&Articles.Views).Default("0") // DEFAULT 0, any SQL expression
b.Column(&Articles.Note).Nullable() // or NotNull()
b.Column(&Articles.Slug).Collation("C").Unique()
b.Column(&Articles.Search).Type("tsvector").Generated("to_tsvector('english', title)")
b.Column(&Articles.Secret).Comment("Never shown").Sensitive()
}

Nullable and NotNull are for a Go type that can't express it, such as a plain string in a nullable column. A field that holds NULL needs a pointer or sql.Null.

Native enums and table comments​

Enum maps a column to a PostgreSQL enum type that migrations create. Comment on the builder describes the table.

func (Article) Configure(b *ant.ModelBuilder[Article]) {
b.Column(&Articles.Status).Enum("post_status", "draft", "live")
b.Comment("Published articles")
}

Add an index​

b.Index starts an index on one column. And adds columns, and the other calls change the index just declared.

func (Article) Configure(b *ant.ModelBuilder[Article]) {
b.Index(&Articles.AuthorID).And(&Articles.CreatedAt).Desc()
// CREATE INDEX articles_author_id_created_at_idx ON articles USING btree (author_id, created_at DESC)
}

Desc applies to the column added last, so only created_at goes down here. The index name is table_columns_idx unless you call Name.

func (Article) Configure(b *ant.ModelBuilder[Article]) {
b.Index(&Articles.Slug).Unique()
// CREATE UNIQUE INDEX articles_slug_idx ON articles USING btree (slug)
}

Partial indexes and index methods​

Where indexes only the rows that match a SQL condition, and Using picks the index method.

func (Article) Configure(b *ant.ModelBuilder[Article]) {
b.Index(&Articles.Published).Where("published IS NOT NULL").Name("articles_live_idx")
// CREATE INDEX articles_live_idx ON articles USING btree (published) WHERE (published IS NOT NULL)

b.Index(&Articles.Tags).Using("gin")
// CREATE INDEX articles_tags_idx ON articles USING gin (tags)
}

Where NULLs sort​

PostgreSQL puts NULLs last in an ascending index and first in a descending one. NullsFirst and NullsLast change that for the column added last, so an ORDER BY ... NULLS FIRST query can use the index. ant writes the clause only when it differs from the default for that direction.

func (Article) Configure(b *ant.ModelBuilder[Article]) {
b.Index(&Articles.Published).NullsFirst().Name("articles_published_idx")
// CREATE INDEX articles_published_idx ON articles USING btree (published NULLS FIRST)

b.Index(&Articles.Views).Desc().NullsLast().Name("articles_views_idx")
// CREATE INDEX articles_views_idx ON articles USING btree (views DESC NULLS LAST)
}

Expressions and covering columns​

IndexExpr indexes a SQL expression such as lower(title). Include stores extra columns in the index.

func (Article) Configure(b *ant.ModelBuilder[Article]) {
b.IndexExpr("lower(title)").Name("articles_title_lower_idx")
// CREATE INDEX articles_title_lower_idx ON articles USING btree (lower((title)::text))

b.Index(&Articles.Body).Include(&Articles.Title)
// CREATE INDEX articles_body_idx ON articles USING btree (body) INCLUDE (title)
}

.Concurrently() runs the migration that adds the index outside a transaction, so the build doesn't block writes.

Add a check constraint​

func (Article) Configure(b *ant.ModelBuilder[Article]) {
b.Check("articles_views_positive", "views >= 0")
// CONSTRAINT articles_views_positive CHECK (views >= 0)
}

Declare relations​

Configure also declares relations that names can't find. See declaring relations.

Other settings​

CallPage
b.Key, b.CompositeKey, b.NoKey, b.KeyNameKeys
b.Discriminator, b.View, b.SchemaShared tables and views
b.FilterGlobal filters
b.ValidateValidation
b.ComparerSaveChanges
b.NoTimestampsTimestamps
b.PartitionByRange, b.PartitionByList, b.PartitionByHashPartitioned tables
b.ExcludeFromMigrationsModels to tables