I'm trying to establish a belongsTo relationship on the same table. But I'm unable to succeed. I didn't find any easily understandable examples on the Internet.
type Role struct {
gorm.Model
Name string
Description string
Inclusive bool
Controller *Role `gorm:"foreignkey:controller;association_foreignkey:id"`
SupervisedRoles []*Role `gorm:"foreignkey:Controller"`
Permissions []*Permission
}
func (r *Role) GetController() (*Role, error) {
controller := &Role{}
return controller, db.DB.Model(r).Related(controller).Error
}
func (r *Role) GetSupervisedRoles() ([]*Role, error) {
roles := []*Role{}
return roles, db.DB.Model(r).Related(roles).Error
}
In my table, the "controller" field is a nullable foreign key that refers to another occurance of my "roles" table.
How do I implement it using GORM ?