英文:
How to create two join tables with go-gorm [Go]
问题
每次运行AutoMigrate()时,我都会收到错误level=fatal msg="failed to found relation: UserFriends"
这是我的用户模型:
type User struct {
UUID string `gorm:"primaryKey;unique;not null" json:"user_id"`
Email string `gorm:"unique;not null" json:"email" binding:"required,email"`
Password string `gorm:"-:all" json:"password"`
DisplayName string `gorm:"unique;not null" json:"display_name" binding:"required"`
FirstName string `gorm:"not null" json:"first_name" binding:"required"`
LastName string `gorm:"not null" json:"last_name" binding:"required"`
PhoneNumber string `gorm:"unique;not null" json:"phone_number" binding:"required"`
DateOfBirth string `gorm:"not null" json:"date_of_birth" binding:"required"`
EmailVerified bool `json:"email_verified"`
PhoneVerified bool `json:"phone_verified"`
PhotoURL string `json:"photo_url" binding:"omitempty,url"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
RefreshToken string `json:"refresh_token"`
Friendship []Friendship `gorm:"many2many:users_friendships;" json:"-"`
}
这是我的友谊模型:
type Friendship struct {
gorm.Model
FromUserId string `gorm:"primaryKey" json:"from_user"`
ToUserId string `gorm:"primaryKey" json:"to_user" binding:"required"`
SentTime time.Time `json:"sent_time"`
ResponseTime time.Time `json:"response_time"`
Accepted bool `json:"accepted"`
}
这是我的用户朋友模型:
type UserFriend struct {
UserUUID string
FriendshipID uint
Accepted bool
}
这是我的迁移代码:
func MigrateDB(db *gorm.DB) error {
err := db.SetupJoinTable(&domain.User{}, "UserFriends", &domain.Friendship{})
if err != nil {
return err
}
err = db.AutoMigrate(&domain.User{}, &domain.Friendship{}, &joins.UserFriend{})
if err != nil {
return err
}
return nil
}
英文:
Every time i run AutoMigrate() I get the error level=fatal msg="failed to found relation: UserFriends"
here is my user model
type User struct {
UUID string `gorm:"primaryKey;unique;not null" json:"user_id"`
Email string `gorm:"unique;not null" json:"email" binding:"required,email"`
Password string `gorm:"-:all" json:"password"`
DisplayName string `gorm:"unique;not null" json:"display_name" binding:"required"`
FirstName string `gorm:"not null" json:"first_name" binding:"required"`
LastName string `gorm:"not null" json:"last_name" binding:"required"`
PhoneNumber string `gorm:"unique;not null" json:"phone_number" binding:"required"`
DateOfBirth string `gorm:"not null" json:"date_of_birth" binding:"required"`
EmailVerified bool `json:"email_verified"`
PhoneVerified bool `json:"phone_verified"`
PhotoURL string `json:"photo_url" binding:"omitempty,url"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
RefreshToken string `json:"refresh_token"`
Friendship []Friendship `gorm:"many2many:users_friendships;"`
}
here is my friendship model
type Friendship struct {
gorm.Model
FromUserId string `gorm:"primaryKey" json:"from_user"`
ToUserId string `gorm:"primaryKey" json:"to_user" binding:"required"`
SentTime time.Time `json:"sent_time"`
ResponseTime time.Time `json:"response_time"`
Accepted bool `json:"accepted"`
}
here is my user friend model
type UserFriend struct {
UserUUID string
FriendshipID uint
Accepted bool
}
and my migration code
func MigrateDB(db *gorm.DB) error {
err := db.SetupJoinTable(&domain.User{}, "UserFriends", &domain.Friendship{})
if err != nil {
return err
}
err = db.AutoMigrate(&domain.User{}, &domain.Friendship{}, &joins.UserFriend{})
if err != nil {
return err
}
return nil
}
答案1
得分: 1
你应该在这里使用"自引用多对多"的关系。这里有一个很好的例子,展示了用户之间的好友关系。
英文:
You should use Self Referential Many to Many
relationship here. Here is a nice example of user friend relationship.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论