title: belongTo self in gorm
body: <p>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.</p>
  
  <pre><code>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 := &amp;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
  }
  </code></pre>
  
  <p>In my table, the "controller" field is a nullable foreign key that refers to another occurance of my "roles" table.</p>
  
  <p>How do I implement it using GORM ?</p>