Skip to main content

Models to tables

Column types​

Go typePostgreSQL column
string, named stringstext
int, int64, uint32, uint64bigint
int32, uint16integer
int16, int8, uint8smallint
boolboolean
float64, float32double precision, real
time.Timetimestamptz
[]bytebytea
[16]byte, such as uuid.UUIDuuid
ant.JSON[T]jsonb
postgres.Array[T]text[], bigint[], and so on

A column is NOT NULL unless its Go type can hold nothing: a pointer, sql.Null[T], sql.NullString and friends, ant.DeletedAt, or a slice. Integer primary keys are generated by the database. Fields tagged ant:"-" or ant:"computed" don't get a column.

When there is no obvious column​

If a type has no obvious column, diff stops and names the field. Set the type in Configure, or have the type declare its own column, which suits a library type.

type Money struct {
Cents int64
}

func (Money) ColumnType(dialect string) string {
return "numeric(19,4)"
}

Indexes​

b.Index takes the columns, and chained calls set the rest. NullsFirst() and NullsLast() set where NULLs sort when that isn't the direction's default.

type Ticket struct {
ID int64
AuthorID int64
CreatedAt time.Time
Views *int64
}

var Tickets = ant.Fields[Ticket]()

func (Ticket) Configure(b *ant.ModelBuilder[Ticket]) {
b.Index(&Tickets.Views).Desc().NullsLast()
b.Index(&Tickets.AuthorID).And(&Tickets.CreatedAt).Desc()
}
CREATE INDEX "tickets_views_idx" ON "tickets" ("views" DESC NULLS LAST);

See models configuration for the rest of the index options.

Relations​

Relations become foreign keys. User.Posts and Post.User are one key, and either side can configure it.

  • Delete behavior is NO ACTION unless you set it.
  • A many-to-many relation creates its join table, once.
  • Foreign keys get an index, unless another index already starts with that column.
  • .NoForeignKey() skips the constraint, and .NoIndex() skips the index.
  • .Morph relations have no foreign key, because one column can't point at several tables.

Enums and sequences​

Migrations create enums and sequences before the tables that use them. drift compares only the enums and sequences the models declare, so a type created by hand doesn't count against you.

type Ticket struct {
ID int64
Status string
Number int64
}

var Tickets = ant.Fields[Ticket]()

func (Ticket) Configure(b *ant.ModelBuilder[Ticket]) {
b.Column(&Tickets.Status).Enum("ticket_status", "open", "closed")
b.Column(&Tickets.Number).Sequence("org_numbers", ant.StartAt(1000))
}
CREATE TYPE "ticket_status" AS ENUM ('open', 'closed');

CREATE SEQUENCE "org_numbers" START WITH 1000;
  • A new enum value is one ALTER TYPE ... ADD VALUE, placed BEFORE the value after it in the models. A file that adds a value and also uses it runs without a transaction.
  • Removing a value, or reordering the existing ones, needs a person. diff writes a warning and no statement.
  • A sequence whose increment or start changes is altered in place and keeps its current value.

Generated SQL shows each case.

A key on several columns​

b.ForeignKey declares a constraint a relation can't express, such as one spanning several columns.

type Line struct {
ID int64
OrderID int64
Region string
}

type Order struct {
ID int64
Region string
}

func (Line) Configure(b *ant.ModelBuilder[Line]) {
b.ForeignKey(ant.F(&Lines.OrderID), ant.F(&Lines.Region)).
References(ant.F(&Orders.ID), ant.F(&Orders.Region)).
OnDelete(ant.Cascade)
}

var (
Lines = ant.Fields[Line]()
Orders = ant.Fields[Order]()
)

Models that are not tables​

CallMeans
b.View("name")No table. Define the view in migrations/code
b.ExcludeFromMigrations()Something else owns this table
b.Schema("sales")Create that schema, and qualify everything

Models on one table merge into one table definition, which is how b.Discriminator works, and a column only some of them declare allows NULL.

Long names​

PostgreSQL cuts a name after 63 bytes, so two long names can become one. diff refuses a name over that limit and says which one. The index and constraint names ant generates are shortened with a hash.