golang?gorm的關(guān)系關(guān)聯(lián)實現(xià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)文章
golang如何設(shè)置Header Content-type
這篇文章主要介紹了golang如何設(shè)置Header Content-type問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01go高并發(fā)時append方法偶現(xiàn)錯誤解決分析
這篇文章主要為大家介紹了go高并發(fā)時append方法偶現(xiàn)錯誤解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10golang中拿slice當(dāng)queue和拿list當(dāng)queue使用分析
這篇文章主要為大家介紹了golang?中拿slice當(dāng)queue和拿list當(dāng)queue使用分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08