亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

我應該如何引用兩個具有單個外鍵的表?

我應該如何引用兩個具有單個外鍵的表?

Go
搖曳的薔薇 2022-08-01 09:35:26
我有一個公司模型,我有一個客戶模型。兩者都可以是一種關系。type Customer struct {    gorm.Model    Title string}type Company struct {    gorm.Model    Title string}type Relation struct {    gorm.Model    CustomerOrCompanyID uint}所以兩者都是一種關系。我怎樣才能讓我的關系指向公司或客戶?
查看完整描述

2 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

有一種方法可以在Gorm中使用多態的Haive One關系自然地做到這一點。這將使您能夠輕松運行@TheSphinX提供的一些查詢。


但是,您需要向關系表添加類型鑒別器列才能使其正常工作:


type Customer struct {

    gorm.Model

    Title string

    Rel   Relation `gorm:"polymorphic:Owner"`

}


type Company struct {

    gorm.Model

    Title string

    Rel   Relation `gorm:"polymorphic:Owner"`

}


type Relation struct {

    gorm.Model

    Foo       string

    OwnerID   uint

    OwnerType string

}

現在,您可以正常創建記錄:


cust := Customer{

    Title: "Cust",

    Rel:   Relation{Foo: "bar"},

}

comp := Company{

    Title: "Comp",

    Rel:   Relation{Foo: "baz"},

}

db.Create(&cust)

db.Create(&comp)

若要運行一個查詢,獲取客戶或公司以及@TehSphinX前兩個查詢中的相關信息,請執行如下操作:


var cust Customer

db.Joins("Rel").First(&cust, 1)

最后一個查詢會更復雜,但也可以使用自定義行結構和聯接來完成:


var rows []struct {

    Relation

    Customer Customer `gorm:"embedded;embeddedPrefix:cust_"`

    Company  Company  `gorm:"embedded;embeddedPrefix:comp_"`

}


db.Select(`

        relations.*,

        customers.id AS cust_id,

        customers.title AS cust_title,

        companies.id AS comp_id,

        companies.title AS comp_title

    `).

    Model(&Relation{}).

    Joins("LEFT JOIN customers ON relations.owner_id = customers.id AND relations.owner_type = 'customers'").

    Joins("LEFT JOIN companies ON relations.owner_id = companies.id AND relations.owner_type = 'companies'").

    Find(&rows)


// now rows[i].Relation is filled, and rows[i].Customer or rows[i].Company 

// are non-zero depending on rows[i].Relation.OwnerType


查看完整回答
反對 回復 2022-08-01
?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

注意:從純SQL的角度來說,因為我還沒有使用過:gorm


在我上面的評論中,我問:


您如何知道 中的 ID 是客戶 ID 還是公司 ID?您是否有一些布爾字段說這是不是客戶?還是 ID 本身包含該信息?喜歡和?CustomerOrCompanyIDcust-1234comp-1234


由于您需要一種方法來區分哪種ID,因此我建議使用兩個字段:和。其中之一始終是(或)。CustomerOrCompanyIDCustomerIDCompanyID0NULL


這樣,您就可以知道要聯接哪個表,也可以同時聯接兩者,具體取決于每行具有的數據類型,將填充客戶或公司的列。


我認為如果有2個字段,應該不會再有問題了,因為它會將它們中的每一個都處理為一個正常的關系,而不是為所有列設置。gorm


一些示例 SQL 語句 (MySQL Dialekt):


所有客戶:


SELECT 

    Relation.ID as RelID, Relation.OtherColumn,

    Customer.ID, Customer.Name

    /* etc. */

FROM 

    Relation INNER JOIN Customer ON (Relation.CustomerID=Customer.ID);

所有公司:


SELECT 

    Relation.ID as RelID, Relation.OtherColumn, 

    Company.ID, Company.Name

    /* etc. */

FROM 

    Relation INNER JOIN Company ON (Relation.CompanyID=Company.ID);

您可能還希望所有數據都與“公司”和“客戶”列一起,并在前端決定使用哪些數據:Relation


SELECT 

    Relation.ID as RelID, 

    Relation.OtherColumn, 

    Customer.ID as CustID,

    Customer.Name as CustName,

    Company.ID as CompID,

    Company.Name as CompName,

    /* etc. */

FROM 

    Relation 

    LEFT JOIN Company ON (Relation.CompanyID=Company.ID)

    LEFT JOIN Customer ON (Relation.CustomerID=Customer.ID);


查看完整回答
反對 回復 2022-08-01
  • 2 回答
  • 0 關注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號