欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

golang?gorm的關(guān)系關(guān)聯(lián)實現(xiàn)示例

 更新時間:2022年04月15日 14:39:18   作者:Jeff的技術(shù)棧  
這篇文章主要為大家介紹了golang?gorm的關(guān)系關(guān)聯(lián)實現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪

1. 關(guān)聯(lián)

1.1. 屬于

// `User`屬于`Profile`, `ProfileID`為外鍵
type User struct {
  gorm.Model
  Profile   Profile
  ProfileID int
}
type Profile struct {
  gorm.Model
  Name string
}
db.Model(&user).Related(&profile)
//// SELECT * FROM profiles WHERE id = 111; // 111是user的外鍵ProfileID

指定外鍵

type Profile struct {
    gorm.Model
    Name string
}
type User struct {
    gorm.Model
    Profile      Profile `gorm:"ForeignKey:ProfileRefer"` // 使用ProfileRefer作為外鍵
    ProfileRefer int
}

指定外鍵和關(guān)聯(lián)外鍵

type Profile struct {
    gorm.Model
    Refer string
    Name  string
}
type User struct {
    gorm.Model
    Profile   Profile `gorm:"ForeignKey:ProfileID;AssociationForeignKey:Refer"`
    ProfileID int
}

1.2. 包含一個

// User 包含一個 CreditCard, UserID 為外鍵
type User struct {
    gorm.Model
    CreditCard   CreditCard
}
type CreditCard struct {
    gorm.Model
    UserID   uint
    Number   string
}
var card CreditCard
db.Model(&user).Related(&card, "CreditCard")
//// SELECT * FROM credit_cards WHERE user_id = 123; // 123 is user's primary key
// CreditCard是user的字段名稱,這意味著獲得user的CreditCard關(guān)系并將其填充到變量
// 如果字段名與變量的類型名相同,如上例所示,可以省略,如:
db.Model(&user).Related(&card)

指定外鍵

type Profile struct {
  gorm.Model
  Name      string
  UserRefer uint
}
type User struct {
  gorm.Model
  Profile Profile `gorm:"ForeignKey:UserRefer"`
}

指定外鍵和關(guān)聯(lián)外鍵

type Profile struct {
  gorm.Model
  Name   string
  UserID uint
}
type User struct {
  gorm.Model
  Refer   string
  Profile Profile `gorm:"ForeignKey:UserID;AssociationForeignKey:Refer"`
}

1.3. 包含多個

// User 包含多個 emails, UserID 為外鍵
type User struct {
    gorm.Model
    Emails   []Email
}
type Email struct {
    gorm.Model
    Email   string
    UserID  uint
}
db.Model(&user).Related(&emails)
//// SELECT * FROM emails WHERE user_id = 111; // 111 是 user 的主鍵

指定外鍵

type Profile struct {
  gorm.Model
  Name      string
  UserRefer uint
}
type User struct {
  gorm.Model
  Profiles []Profile `gorm:"ForeignKey:UserRefer"`
}

指定外鍵和關(guān)聯(lián)外鍵

type Profile struct {
  gorm.Model
  Name   string
  UserID uint
}
type User struct {
  gorm.Model
  Refer   string
  Profiles []Profile `gorm:"ForeignKey:UserID;AssociationForeignKey:Refer"`
}

1.4. 多對多

// User 包含并屬于多個 languages, 使用 `user_languages` 表連接
type User struct {
    gorm.Model
    Languages         []Language `gorm:"many2many:user_languages;"`
}
type Language struct {
    gorm.Model
    Name string
}
db.Model(&user).Related(&languages, "Languages")
//// SELECT * FROM "languages" INNER JOIN "user_languages" ON "user_languages"."language_id" = "languages"."id" WHERE "user_languages"."user_id" = 111

指定外鍵和關(guān)聯(lián)外鍵

type CustomizePerson struct {
  IdPerson string             `gorm:"primary_key:true"`
  Accounts []CustomizeAccount `gorm:"many2many:PersonAccount;ForeignKey:IdPerson;AssociationForeignKey:IdAccount"`
}
type CustomizeAccount struct {
  IdAccount string `gorm:"primary_key:true"`
  Name      string
}

譯者注:這里設(shè)置好像缺失一部分

1.5. 多種包含

支持多種的包含一個和包含多個的關(guān)聯(lián)

type Cat struct {
    Id    int
    Name  string
    Toy   Toy `gorm:"polymorphic:Owner;"`
  }
  type Dog struct {
    Id   int
    Name string
    Toy  Toy `gorm:"polymorphic:Owner;"`
  }
  type Toy struct {
    Id        int
    Name      string
    OwnerId   int
    OwnerType string
  }

注意:多態(tài)屬性和多對多顯式不支持,并且會拋出錯誤。

1.6. 關(guān)聯(lián)模式

關(guān)聯(lián)模式包含一些幫助方法來處理關(guān)系事情很容易。

// 開始關(guān)聯(lián)模式
var user User
db.Model(&user).Association("Languages")
// `user`是源,它需要是一個有效的記錄(包含主鍵)
// `Languages`是關(guān)系中源的字段名。
// 如果這些條件不匹配,將返回一個錯誤,檢查它:
// db.Model(&user).Association("Languages").Error
// Query - 查找所有相關(guān)關(guān)聯(lián)
db.Model(&user).Association("Languages").Find(&languages)
// Append - 添加新的many2many, has_many關(guān)聯(lián), 會替換掉當(dāng)前 has_one, belongs_to關(guān)聯(lián)
db.Model(&user).Association("Languages").Append([]Language{languageZH, languageEN})
db.Model(&user).Association("Languages").Append(Language{Name: "DE"})
// Delete - 刪除源和傳遞的參數(shù)之間的關(guān)系,不會刪除這些參數(shù)
db.Model(&user).Association("Languages").Delete([]Language{languageZH, languageEN})
db.Model(&user).Association("Languages").Delete(languageZH, languageEN)
// Replace - 使用新的關(guān)聯(lián)替換當(dāng)前關(guān)聯(lián)
db.Model(&user).Association("Languages").Replace([]Language{languageZH, languageEN})
db.Model(&user).Association("Languages").Replace(Language{Name: "DE"}, languageEN)
// Count - 返回當(dāng)前關(guān)聯(lián)的計數(shù)
db.Model(&user).Association("Languages").Count()
// Clear - 刪除源和當(dāng)前關(guān)聯(lián)之間的關(guān)系,不會刪除這些關(guān)聯(lián)
db.Model(&user).Association("Languages").Clear()

以上就是golang gorm的關(guān)系關(guān)聯(lián)實現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于golang gorm的關(guān)系關(guān)聯(lián)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • go語言中slice,map,channl底層原理

    go語言中slice,map,channl底層原理

    這篇文章主要介紹了go語言中slice,map,channl底層原理,slice,map,channl是我們Go語言中最最常用的幾個數(shù)據(jù)結(jié)構(gòu),對于其更多相關(guān)內(nèi)容需要的小伙伴可以參考下面文章詳細(xì)內(nèi)容
    2022-06-06
  • golang如何設(shè)置Header Content-type

    golang如何設(shè)置Header Content-type

    這篇文章主要介紹了golang如何設(shè)置Header Content-type問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • go高并發(fā)時append方法偶現(xiàn)錯誤解決分析

    go高并發(fā)時append方法偶現(xiàn)錯誤解決分析

    這篇文章主要為大家介紹了go高并發(fā)時append方法偶現(xiàn)錯誤解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • go mock模擬接口的實現(xiàn)

    go mock模擬接口的實現(xiàn)

    本文主要介紹了go mock模擬接口的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • go code review 代碼調(diào)試

    go code review 代碼調(diào)試

    這篇文章主要為大家介紹了go code review 代碼調(diào)試方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • 揭秘Go Json.Unmarshal精度丟失之謎

    揭秘Go Json.Unmarshal精度丟失之謎

    我們知道在json反序列化時是沒有整型和浮點型的區(qū)別,數(shù)字都使用同一種類型,在go語言的類型中這種共同類型就是float64,下面我們就來探討一下Json.Unmarshal精度丟失之謎吧
    2023-08-08
  • 淺談golang并發(fā)操作變量安全的問題

    淺談golang并發(fā)操作變量安全的問題

    這篇文章主要介紹了淺談golang并發(fā)操作變量安全的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • 如何利用Go語言實現(xiàn)LRU?Cache

    如何利用Go語言實現(xiàn)LRU?Cache

    這篇文章主要介紹了如何利用Go語言實現(xiàn)LRU?Cache,LRU是Least?Recently?Used的縮寫,是一種操作系統(tǒng)中常用的頁面置換算法,下面我們一起進(jìn)入文章了解更多內(nèi)容吧,需要的朋友可以參考一下
    2022-03-03
  • golang中拿slice當(dāng)queue和拿list當(dāng)queue使用分析

    golang中拿slice當(dāng)queue和拿list當(dāng)queue使用分析

    這篇文章主要為大家介紹了golang?中拿slice當(dāng)queue和拿list當(dāng)queue使用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Golang實現(xiàn)密碼加密的示例詳解

    Golang實現(xiàn)密碼加密的示例詳解

    數(shù)據(jù)庫在存儲密碼時,不能明文存儲,需要加密后存儲,而Golang中的加密算法有很多種,下面小編就來通過簡單的示例和大家簡單聊聊吧
    2023-07-07

最新評論