網站首頁 編程語言 正文
1. 關聯
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 }
指定外鍵和關聯外鍵
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關系并將其填充到變量 // 如果字段名與變量的類型名相同,如上例所示,可以省略,如: db.Model(&user).Related(&card)
指定外鍵
type Profile struct { gorm.Model Name string UserRefer uint } type User struct { gorm.Model Profile Profile `gorm:"ForeignKey:UserRefer"` }
指定外鍵和關聯外鍵
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"` }
指定外鍵和關聯外鍵
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
指定外鍵和關聯外鍵
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 }
譯者注:這里設置好像缺失一部分
1.5. 多種包含
支持多種的包含一個和包含多個的關聯
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 }
注意:多態屬性和多對多顯式不支持,并且會拋出錯誤。
1.6. 關聯模式
關聯模式包含一些幫助方法來處理關系事情很容易。
// 開始關聯模式 var user User db.Model(&user).Association("Languages") // `user`是源,它需要是一個有效的記錄(包含主鍵) // `Languages`是關系中源的字段名。 // 如果這些條件不匹配,將返回一個錯誤,檢查它: // db.Model(&user).Association("Languages").Error // Query - 查找所有相關關聯 db.Model(&user).Association("Languages").Find(&languages) // Append - 添加新的many2many, has_many關聯, 會替換掉當前 has_one, belongs_to關聯 db.Model(&user).Association("Languages").Append([]Language{languageZH, languageEN}) db.Model(&user).Association("Languages").Append(Language{Name: "DE"}) // Delete - 刪除源和傳遞的參數之間的關系,不會刪除這些參數 db.Model(&user).Association("Languages").Delete([]Language{languageZH, languageEN}) db.Model(&user).Association("Languages").Delete(languageZH, languageEN) // Replace - 使用新的關聯替換當前關聯 db.Model(&user).Association("Languages").Replace([]Language{languageZH, languageEN}) db.Model(&user).Association("Languages").Replace(Language{Name: "DE"}, languageEN) // Count - 返回當前關聯的計數 db.Model(&user).Association("Languages").Count() // Clear - 刪除源和當前關聯之間的關系,不會刪除這些關聯 db.Model(&user).Association("Languages").Clear()
原文鏈接:https://www.cnblogs.com/guyouyin123/p/14115312.html
相關推薦
- 2022-11-05 C/C++讀取大文件數據方式詳細講解_C 語言
- 2022-06-09 Python字符串格式化方式_python
- 2022-02-17 使用Postman測試接口提示Error: connect ECONNREFUSED 127.0.0
- 2022-04-07 一篇文章帶你學習Python3的高階函數_python
- 2022-03-29 詳解python字符串相關str_python
- 2023-10-16 獲取當月的月初和月末日期時間戳
- 2022-07-25 python數據清洗中的時間格式化實現_python
- 2022-11-21 Android?Jetpack系列之App?Startup使用詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支