第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

golang gorm 將兩列引用到同一個表,插入問題

golang gorm 將兩列引用到同一個表,插入問題

Go
慕森王 2022-12-13 11:00:59
最初的想法是從 yii 構(gòu)建 rbac 模擬https://github.com/yiisoft/yii2/blob/master/framework/rbac/migrations/schema-pgsql.sql所以,我有這兩個模型: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"`}我也已經(jīng)在 auth_items 表中有一些數(shù)據(jù),我想用 GORM 插入到 auth_relations 表中,它看起來像這樣:var relation = models.AuthRelations{    Parent: models.AuthItem{ID: 1},    Child:  models.AuthItem{ID: 2},}err = db.Save(&relation).Errorif err != nil {    log.Fatalf("cant insert: %v", err)}我收到此錯誤:failed to set value 0x1 to field Parent; failed to set value 0x1 to field Parent 我嘗試使用 gorm 函數(shù) Value(),類似于:func (item AuthItem) Value() (driver.Value, error) {    return int64(item.ID), nil }在我實現(xiàn)這個功能后 db.Save 工作,但約束/外鍵/引用停止工作所以我的問題是:是否有任何選項可以以正確的方式建立這樣的關(guān)系,或者我如何在不失去約束的情況下使用 value() 函數(shù)?
查看完整描述

2 回答

?
胡說叔叔

TA貢獻1804條經(jīng)驗 獲得超8個贊

關(guān)系更接近one-to-many或many-to-many有一個父母可以有多個孩子的


由于我們指的是與孩子相同的類型,我們可以按如下方式更新模型:


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可以插入為


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)

Self-Referential-Has-Many同樣可以在我們不需要第二張表的地方實現(xiàn),并且可以使用帶有一個額外列的同一張表作為參考


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"`

}

我們可以像以前一樣插入記錄


查看完整回答
反對 回復(fù) 2022-12-13
?
拉丁的傳說

TA貢獻1789條經(jīng)驗 獲得超8個贊

我得出結(jié)論,我需要使用 many2many 關(guān)系,這里是案例的例子:


type AuthItem struct {

    ID          uint      `gorm:"uniqueIndex;primaryKey;auto_increment" json:"id"`

    Name        string    `gorm:"unique;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"`

    CreatedAt   time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`

    UpdatedAt   time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`

    DeletedAt   time.Time `gorm:"default:null" json:"deleted_at"`

}


type AuthRelations struct {

    ID        uint       `gorm:"uniqueIndex;primaryKey;auto_increment" json:"id"`

    Parent    []AuthItem `gorm:"many2many:prnt_authitem_parent;References:Name;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"parent"`

    Child     []AuthItem `gorm:"many2many:chld_authitem_child;References:Name;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"child"`

    CreatedAt time.Time  `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`

    UpdatedAt time.Time  `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`

    DeletedAt time.Time  `gorm:"default:null" json:"deleted_at"`

}


查看完整回答
反對 回復(fù) 2022-12-13
  • 2 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號