Relations
Relations define how models are connected to each other. forge supports ForeignKey, OneToOne, and ManyToMany relationships (one-to-many is represented by a ForeignKey with a RelatedName).
Complete reference for model relationships.
Relation Types​
ForeignKey (Many-to-One)​
A many-to-one relationship:
import "github.com/forgego/forge/schema"
func (Post) Relations() []schema.Relation {
return []schema.Relation{
schema.ForeignKeyField("author", "User",
schema.OnDelete(schema.CascadeCASCADE),
schema.RelatedName("posts"),
),
}
}
Access:
post.Author // *User
user.Posts // []*Post (reverse)
OneToOne​
A one-to-one relationship:
func (User) Relations() []schema.Relation {
return []schema.Relation{
schema.OneToOneField("profile", "UserProfile",
schema.OnDelete(schema.CascadeCASCADE),
),
}
}
Access:
user.Profile // *UserProfile
profile.User // *User (reverse)
ManyToMany​
A many-to-many relationship:
func (Post) Relations() []schema.Relation {
return []schema.Relation{
schema.ManyToManyField("tags", "Tag",
schema.Through("post_tags"),
schema.RelatedName("posts"),
),
}
}
Access:
post.Tags // []*Tag
tag.Posts // []*Post (reverse)
Relation Options​
OnDelete​
Cascade behavior on delete:
schema.ForeignKeyField("author", "User",
schema.OnDelete(schema.CascadeCASCADE),
// schema.OnDelete(schema.CascadeSET_NULL),
// schema.OnDelete(schema.CascadeSET_DEFAULT),
// schema.OnDelete(schema.CascadePROTECT), // Prevent deletion
// schema.OnDelete(schema.CascadeDO_NOTHING), // No action
)
OnUpdate​
Cascade behavior on update:
schema.ForeignKeyField("author", "User",
schema.OnUpdate(schema.CascadeCASCADE),
)
RelatedName​
Reverse relation name:
schema.ForeignKeyField("author", "User",
schema.RelatedName("posts"),
)
Through​
Through table for ManyToMany:
schema.ManyToManyField("tags", "Tag",
schema.Through("post_tags"),
)
DBConstraint​
Control FK constraint creation:
schema.ForeignKeyField("author", "User",
schema.DBConstraint(true),
)
Cascade Options​
| Option | SQL | Description |
|---|---|---|
CascadeCASCADE | ON DELETE CASCADE | Delete related objects |
CascadeSET_NULL | ON DELETE SET NULL | Set foreign key to NULL |
CascadeSET_DEFAULT | ON DELETE SET DEFAULT | Set foreign key to default |
CascadePROTECT | ON DELETE RESTRICT | Prevent deletion if related objects exist |
CascadeDO_NOTHING | ON DELETE NO ACTION | No action (database default) |
Examples​
Blog Post with Author​
type Post struct {
schema.BaseSchema
}
func (Post) Relations() []schema.Relation {
return []schema.Relation{
schema.ForeignKeyField("author", "User",
schema.OnDelete(schema.CascadeCASCADE),
schema.RelatedName("posts"),
),
}
}
User with Profile​
type User struct {
schema.BaseSchema
}
func (User) Relations() []schema.Relation {
return []schema.Relation{
schema.OneToOneField("profile", "UserProfile",
schema.OnDelete(schema.CascadeCASCADE),
),
}
}
Post with Tags​
type Post struct {
schema.BaseSchema
}
func (Post) Relations() []schema.Relation {
return []schema.Relation{
schema.ManyToManyField("tags", "Tag",
schema.Through("post_tags"),
schema.RelatedName("posts"),
),
}
}
See Also​
- Models Guide - Model usage guide
- Queries Guide - Querying relations