'golang gorm reference two columns to same table, problem with insert

original idea was to build rbac analog from yii https://github.com/yiisoft/yii2/blob/master/framework/rbac/migrations/schema-pgsql.sql

so, i have these two models:

type AuthItem struct {
    ID          uint   `gorm:"uniqueIndex;primaryKey;auto_increment;column:id" json:"id"`
    Name        string `gorm:"uniqueIndex;primaryKey;not null;type:varchar(64);column:name" json:"name"`
    ItemType    int64  `gorm:"type:smallint;not null;column:item_type" json:"item_type"`
    Description string `gorm:"size:255;column:description" json:"description"`
}

type AuthRelations struct {
    gorm.Model
    Parent AuthItem `gorm:"references:id;foreignKey:parent;column:parent" json:"parent"`
    Child  AuthItem `gorm:"references:id;foreignKey:child;column:child" json:"child"`
}

also i already have some data in auth_items table and i want to make insert into auth_relations table with GORM, and its looks like this:

var relation = models.AuthRelations{
    Parent: models.AuthItem{ID: 1},
    Child:  models.AuthItem{ID: 2},
}

err = db.Save(&relation).Error
if err != nil {
    log.Fatalf("cant insert: %v", err)
}

i getting this error:

failed to set value 0x1 to field Parent; failed to set value 0x1 to field Parent 

i tried to use gorm function Value(), something like:

func (item AuthItem) Value() (driver.Value, error) {
    return int64(item.ID), nil 
}

and after i implement this function db.Save works, but the constraints/foreignKeys/references stop working

so my question: is there any options to make relations like this in right way or how can i use value() function without loosing constraints ?



Solution 1:[1]

The relation is more near to one-to-many or many-to-many has one parent can have multiple children's

Since we are referring to the same type as children we can update the model as below:

type AuthItem struct {
    ID          uint   `gorm:"primaryKey; column:id" json:"id"`
    Name        string `gorm:"primaryKey; not null; type:varchar(64); column:name" json:"name"`
    ItemType    int64  `gorm:"type:smallint; not null; column:item_type" json:"item_type"`
    Description string `gorm:"size:255; column:description" json:"description"`

    AuthRelations []AuthItem `gorm:"many2many:auth_relations; foreignKey:ID; joinForeignKey:Parent; References:ID; joinReferences:Child; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}

AuthItem can be inserted as

var relation = []AuthItem{
  {
    ID: 1,
    Name: "super",
    AuthRelations: []AuthItem{
      { ID: 2, Name: "admin" },
      { ID: 3, Name: "owner" },
    },
  }, {
    ID: 2,
    Name: "user",
    AuthRelations: []AuthItem{
      { ID: 3, Name: "normal" },
      { ID: 5, Name: "client" },
    },
  },
}
err = db.Save(&relation).Error
if err != nil {
  log.Fatalf("cant insert: %v", err)
}
log.Printf("Relation: %#v\n", &relation)

Same can be achieved with Self-Referential-Has-Many where we don't require second table and can use the same table with one extra column as reference

type AuthItem struct {
    ID          uint   `gorm:"primaryKey; column:id" json:"id"`
    Name        string `gorm:"primaryKey; not null; type:varchar(64); column:name" json:"name"`
    ItemType    int64  `gorm:"type:smallint; not null; column:item_type" json:"item_type"`
    Description string `gorm:"size:255; column:description" json:"description"`

    Parent *uint `gorm:"column: parent;" json:"parent"`
    AuthRelations []AuthItem `gorm:"foreignKey:Parent"; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}

we can insert record the same manner as we did before

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chandan