Declaring relations
A relation is a field on the struct: []Post is one-to-many, *User is many-to-one or one-to-one. ant finds the foreign key by name.
Declare a relation by name
type Person struct {
ID int64
Name string
Passport *Passport // one-to-one, through Passport.PersonID
Pets []Pet // one-to-many, through Pet.PersonID
}
type Passport struct {
ID int64
PersonID int64
Number string
}
type Pet struct {
ID int64
PersonID int64
Name string
Person *Person // many-to-one, through Pet.PersonID
}
The foreign key is the owner's name plus ID: Pet.PersonID joins to Person.ID. Relation fields aren't columns and stay empty until you load them. A relation can be declared on one side only and used from that side.
users, err := db.Users.Include(&Users.Posts).All(ctx)
// SELECT ... FROM "users"
// SELECT ... FROM "posts" WHERE "posts"."user_id" IN ($1, $2, $3)
Name the foreign key
When the name rule doesn't fit, name the key in Configure, on either side.
type Author struct {
ID int64
Handle string
Books []Book
}
func (Author) Configure(b *ant.ModelBuilder[Author]) {
b.HasMany(&Authors.Books).ForeignKey(&Books.WriterID).OnDelete(ant.Cascade)
}
type Book struct {
ID int64
WriterID int64
Title string
Writer *Author
}
func (Book) Configure(b *ant.ModelBuilder[Book]) {
b.BelongsTo(&Books.Writer).ForeignKey(&Books.WriterID)
}
var (
Authors = ant.Fields[Author]()
Books = ant.Fields[Book]()
)
| Call | Declares |
|---|---|
b.HasMany(&Users.Posts) | one-to-many |
b.HasOne(&Users.Passport) | one-to-one, key on the other side |
b.BelongsTo(&Posts.User) | many-to-one, key on this side |
b.ManyToMany(&Users.Roles) | through a join table |
A relation that nothing connects returns ant.ErrNoForeignKey at the terminal call (All, First and so on), not when the model is declared.
Two relations to the same model, such as Posts []Article and Edited []Article on Author, would both be found through Article.AuthorID and read each other's rows, so both return ant.ErrNoForeignKey until one of them names its key. A relation scoped with .Where, .Latest or .Oldest shares the key on purpose, and is not refused.
Join on a column other than the key
References joins on another column of the owner, which must be unique, such as a handle or a SKU.
type Quote struct {
ID int64
AuthorHandle string
Text string
}
func (Author) Configure(b *ant.ModelBuilder[Author]) {
b.HasMany(&Authors.Quotes).ForeignKey(&Quotes.AuthorHandle).References(&Authors.Handle)
}
var Quotes = ant.Fields[Quote]()
Choose what happens on delete
The foreign key is NO ACTION unless set, so deleting a parent that still has children fails.
func (Post) Configure(b *ant.ModelBuilder[Post]) {
b.BelongsTo(&Posts.User).OnDelete(ant.Cascade).OnUpdate(ant.NoAction)
}
The actions are ant.Cascade (delete the children too), ant.SetNull, ant.Restrict, and ant.NoAction. They become part of the foreign key in migrations, and the database enforces them.
Skip the constraint or the index
func (Post) Configure(b *ant.ModelBuilder[Post]) {
b.BelongsTo(&Posts.User).NoForeignKey() // keep the relation, skip the constraint
b.BelongsTo(&Posts.User).NoIndex() // skip the automatic index
}
Point a model at itself
The parent is found through ParentID, the children through the back-reference.
type Folder struct {
ID int64
ParentID *int64
Parent *Folder
Children []Folder
Name string
}
Use a pointer key, so a root can have no parent. For many-to-many, declare it through a join table.
See it tested: tests/features/relations