关联

属于 {#bt}

  1. // `User`属于`Profile`, `ProfileID`为外键
  2. type User struct {
  3. gorm.Model
  4. Profile Profile
  5. ProfileID int
  6. }
  7. type Profile struct {
  8. gorm.Model
  9. Name string
  10. }
  11. db.Model(&user).Related(&profile)
  12. //// SELECT * FROM profiles WHERE id = 111; // 111是user的外键ProfileID

指定外键

  1. type Profile struct {
  2. gorm.Model
  3. Name string
  4. }
  5. type User struct {
  6. gorm.Model
  7. Profile Profile `gorm:"ForeignKey:ProfileRefer"` // 使用ProfileRefer作为外键
  8. ProfileRefer int
  9. }

指定外键和关联外键

  1. type Profile struct {
  2. gorm.Model
  3. Refer string
  4. Name string
  5. }
  6. type User struct {
  7. gorm.Model
  8. Profile Profile `gorm:"ForeignKey:ProfileID;AssociationForeignKey:Refer"`
  9. ProfileID int
  10. }

包含一个 {#ho}

  1. // User 包含一个 CreditCard, UserID 为外键
  2. type User struct {
  3. gorm.Model
  4. CreditCard CreditCard
  5. }
  6. type CreditCard struct {
  7. gorm.Model
  8. UserID uint
  9. Number string
  10. }
  11. var card CreditCard
  12. db.Model(&user).Related(&card, "CreditCard")
  13. //// SELECT * FROM credit_cards WHERE user_id = 123; // 123 is user's primary key
  14. // CreditCard是user的字段名称,这意味着获得user的CreditCard关系并将其填充到变量
  15. // 如果字段名与变量的类型名相同,如上例所示,可以省略,如:
  16. db.Model(&user).Related(&card)

指定外键

  1. type Profile struct {
  2. gorm.Model
  3. Name string
  4. UserRefer uint
  5. }
  6. type User struct {
  7. gorm.Model
  8. Profile Profile `gorm:"ForeignKey:UserRefer"`
  9. }

指定外键和关联外键

  1. type Profile struct {
  2. gorm.Model
  3. Name string
  4. UserID uint
  5. }
  6. type User struct {
  7. gorm.Model
  8. Refer string
  9. Profile Profile `gorm:"ForeignKey:UserID;AssociationForeignKey:Refer"`
  10. }

包含多个 {#hm}

  1. // User 包含多个 emails, UserID 为外键
  2. type User struct {
  3. gorm.Model
  4. Emails []Email
  5. }
  6. type Email struct {
  7. gorm.Model
  8. Email string
  9. UserID uint
  10. }
  11. db.Model(&user).Related(&emails)
  12. //// SELECT * FROM emails WHERE user_id = 111; // 111 是 user 的主键

指定外键

  1. type Profile struct {
  2. gorm.Model
  3. Name string
  4. UserRefer uint
  5. }
  6. type User struct {
  7. gorm.Model
  8. Profiles []Profile `gorm:"ForeignKey:UserRefer"`
  9. }

指定外键和关联外键

  1. type Profile struct {
  2. gorm.Model
  3. Name string
  4. UserID uint
  5. }
  6. type User struct {
  7. gorm.Model
  8. Refer string
  9. Profiles []Profile `gorm:"ForeignKey:UserID;AssociationForeignKey:Refer"`
  10. }

多对多 {#mtm}

  1. // User 包含并属于多个 languages, 使用 `user_languages` 表连接
  2. type User struct {
  3. gorm.Model
  4. Languages []Language `gorm:"many2many:user_languages;"`
  5. }
  6. type Language struct {
  7. gorm.Model
  8. Name string
  9. }
  10. db.Model(&user).Related(&languages, "Languages")
  11. //// SELECT * FROM "languages" INNER JOIN "user_languages" ON "user_languages"."language_id" = "languages"."id" WHERE "user_languages"."user_id" = 111

指定外键和关联外键

  1. type CustomizePerson struct {
  2. IdPerson string `gorm:"primary_key:true"`
  3. Accounts []CustomizeAccount `gorm:"many2many:PersonAccount;ForeignKey:IdPerson;AssociationForeignKey:IdAccount"`
  4. }
  5. type CustomizeAccount struct {
  6. IdAccount string `gorm:"primary_key:true"`
  7. Name string
  8. }

译者注:这里设置好像缺失一部分

多种包含 {#p}

支持多种的包含一个和包含多个的关联

  1. type Cat struct {
  2. Id int
  3. Name string
  4. Toy Toy `gorm:"polymorphic:Owner;"`
  5. }
  6. type Dog struct {
  7. Id int
  8. Name string
  9. Toy Toy `gorm:"polymorphic:Owner;"`
  10. }
  11. type Toy struct {
  12. Id int
  13. Name string
  14. OwnerId int
  15. OwnerType string
  16. }

注意:多态属性和多对多显式不支持,并且会抛出错误。

关联模式 {#am}

关联模式包含一些帮助方法来处理关系事情很容易。

  1. // 开始关联模式
  2. var user User
  3. db.Model(&user).Association("Languages")
  4. // `user`是源,它需要是一个有效的记录(包含主键)
  5. // `Languages`是关系中源的字段名。
  6. // 如果这些条件不匹配,将返回一个错误,检查它:
  7. // db.Model(&user).Association("Languages").Error
  8. // Query - 查找所有相关关联
  9. db.Model(&user).Association("Languages").Find(&languages)
  10. // Append - 添加新的many2many, has_many关联, 会替换掉当前 has_one, belongs_to关联
  11. db.Model(&user).Association("Languages").Append([]Language{languageZH, languageEN})
  12. db.Model(&user).Association("Languages").Append(Language{Name: "DE"})
  13. // Delete - 删除源和传递的参数之间的关系,不会删除这些参数
  14. db.Model(&user).Association("Languages").Delete([]Language{languageZH, languageEN})
  15. db.Model(&user).Association("Languages").Delete(languageZH, languageEN)
  16. // Replace - 使用新的关联替换当前关联
  17. db.Model(&user).Association("Languages").Replace([]Language{languageZH, languageEN})
  18. db.Model(&user).Association("Languages").Replace(Language{Name: "DE"}, languageEN)
  19. // Count - 返回当前关联的计数
  20. db.Model(&user).Association("Languages").Count()
  21. // Clear - 删除源和当前关联之间的关系,不会删除这些关联
  22. db.Model(&user).Association("Languages").Clear()