在使用GORM進行資料庫操作時,我們常常會遇到一張表需要同時關聯兩個外鍵的情況。在這種情況下,如何正確地設定和使用外鍵成為了一個需要解決的問題。在本文中,php小編魚仔將為大家詳細介紹如何使用GORM來處理一張表的兩個外鍵,以及相關的注意事項和實際應用案例。透過學習本文,相信大家能夠更理解並運用GORM進行資料庫操作。
我想建立三個表格:使用者、事件、配對。
在配對表中將有三列:eventId、user1、user2。 所以user1、user2都會透過id引用Users表。
我知道如何在 sql 中執行此操作,但我想嘗試使用 ORM。我已閱讀文檔,但我不知道如何做到這一點
type User struct { Id uint64 `gorm:"primarykey"` TGtag string IsActive bool } type RCEvent struct { Id uint64 `gorm:"primarykey"` DateStarted time.Time IsActive bool } type Pair struct { EventId uint64 UserID1 uint64 UserID2 uint64 }
我嘗試用 gorm 標籤做一些事情,例如這樣,但沒有成功:
type User struct { Id uint64 `gorm:"primarykey"` TGtag string IsActive bool } type RCEvent struct { Id uint64 `gorm:"primarykey"` DateStarted time.Time IsActive bool Pairs []Pair `gorm:"many2many:pair;"` } type Pair struct { EventId uint64 `gorm:"primarykey"` UserID1 uint64 `gorm:"primarykey"` UserID2 uint64 `gorm:"primarykey"` }
另外,我嘗試了類似的方法並遇到了運行時錯誤
type User struct { Id uint64 `gorm:"primarykey"` TGtag string IsActive bool } type RCEvent struct { Id uint64 `gorm:"primarykey"` DateStarted time.Time IsActive bool } type Pair struct { Event RCEvent User1 User //`gorm:"foreignkey:UserId"` User2 User //`gorm:"foreignkey:UserId"` }
[error] failed to parse value &db.Pair{Event:db.RCEvent{Id:0x0, DateStarted:time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC), IsActive:false}, User1:db.User{Id:0x0, TGtag:"", IsActive:false}, User2:db.User{Id:0x0, TGtag:"", IsActive:false}}, got error invalid field found for struct untitledPetProject/internal/db.Pair's field Event: define a valid foreign key for relations or implement the Valuer/Scanner interface
要使用另一個結構作為外鍵,您應該將其id 添加為目標結構中的另一個字段,對於您的情況,這裡是工作示例(使用gorm 範例):
#package main import ( "log" "time" "gorm.io/driver/sqlite" "gorm.io/gorm" ) type User struct { gorm.Model ID uint64 `gorm:"primarykey"` TGtag string IsActive bool } type RCEvent struct { gorm.Model ID uint64 `gorm:"primarykey"` DateStarted time.Time IsActive bool } type Pair struct { gorm.Model ID uint64 `gorm:"primarykey"` EventID uint64 Event RCEvent User1ID uint64 User1 User User2ID uint64 User2 User } func main() { db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{}) if err != nil { panic("failed to connect database") } // Migrate the schema err = db.AutoMigrate(&User{}) if err != nil { log.Fatal(err) } err = db.AutoMigrate(&RCEvent{}) if err != nil { log.Fatal(err) } err = db.AutoMigrate(&Pair{}) if err != nil { log.Fatal(err) } // Create user1 := User{ID: 2, TGtag: "mohammad"} user2 := User{ID: 3, TGtag: "ali"} event := RCEvent{ID: 2} pair := Pair{ID: 2, EventID: 2, Event: event, User1ID: 2, User1: user1, User2ID: 3, User2: user2} db.Create(&user1) db.Create(&user2) db.Create(&event) db.Create(&pair) // Read var user User db.First(&user, 3) // find product with integer primary key db.First(&user, "t_gtag = ?", "ali") // find product with code D42 // Update - update product's price to 200 db.Model(&user).Update("is_active", false) // Update - update multiple fields db.Model(&user).Updates(User{ID: 4, TGtag: "ahmad"}) // non-zero fields db.Model(&user).Updates(map[string]interface{}{"Id": 5, "TGtag": "hasan"}) // Delete - delete product db.Delete(&user, 2) }
以上是GORM:一張表的兩個外鍵的詳細內容。更多資訊請關注PHP中文網其他相關文章!