我想创建三个表:用户、事件、配对.
在配对表中将有三列:EventID、User1、User2. 因此,user1和user2都将通过id引用USERS表.
我知道如何在SQL中做到这一点,但我想try 使用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-TAG做点什么,就像这样,但没有成功:
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"`
}
此外,我try 了类似的操作,但得到了运行时错误
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