Relationships
Nimbus auto-infers relationships from Go struct conventions — no tags needed.
A UserID field + *User field = belongsTo.
A []Post where Post has UserID = hasMany.
A []Team where Team has no UserID = manyToMany.
Use database.Load or
database.AutoPreload to eager load associations.
belongsTo
A post belongs to a user (auto-inferred from UserID + *User):
type Post struct {
database.Model
UserID uint
User *User
}
hasMany
A user has many posts (auto-inferred because Post has UserID):
type User struct {
database.Model
Posts []Post
}
hasOne
A user has one profile (auto-inferred — single struct, no ProfileID on User):
type User struct {
database.Model
Profile *Profile
}
type Profile struct {
database.Model
UserID uint
User *User
Bio string
}
manyToMany
Users and teams via pivot table (auto-inferred because Team has no UserID):
type User struct {
database.Model
Teams []Team
}
type Team struct {
database.Model
Name string
Users []User
}
Eager loading (Preload)
var posts []Post
database.Preload(database.Get(), "User").Find(&posts)
// or let Nimbus inspect relations and preload automatically:
database.AutoPreload(database.Get(), &Post{}).Find(&posts)
// Each post.User is populated
Nested preload
database.Get().
Preload("User.Profile").
Preload("Comments").
Find(&posts)
Overriding conventions
Use nimbus tags only when you need custom FK names or pivot tables:
type Post struct {
database.Model
AuthorID uint
Author *User ` + "`nimbus:\"belongsTo,foreignKey:AuthorID\"`" + `
}
type User struct {
database.Model
Teams []Team ` + "`nimbus:\"manyToMany,pivotTable:memberships\"`" + `
}
Declaring relations with methods
For more control, models can implement Relations() []string
and let Nimbus auto-preload the listed associations:
func (Post) Relations() []string {
return []string{"User", "Comments"}
}