首頁 > 軟體

golang gorm的關係關聯實現範例

2022-04-15 19:01:16

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()

以上就是golang gorm的關係關聯實現範例的詳細內容,更多關於golang gorm的關係關聯的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com