Models to tables
Column types
| Go type | PostgreSQL column |
|---|---|
string, named strings | text |
int, int64, uint32, uint64 | bigint |
int32, uint16 | integer |
int16, int8, uint8 | smallint |
bool | boolean |
float64, float32 | double precision, real |
time.Time | timestamptz |
[]byte | bytea |
[16]byte, such as uuid.UUID | uuid |
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 ACTIONunless 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..Morphrelations 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, placedBEFOREthe 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.
diffwrites 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
| Call | Means |
|---|---|
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.
See it tested: tests/features/migrations/models_test.go