How to create two join tables with go-gorm [Go]

huangapple go评论127阅读模式
英文:

How to create two join tables with go-gorm [Go]

问题

每次运行AutoMigrate()时,我都会收到错误level=fatal msg="failed to found relation: UserFriends"

这是我的用户模型:

  1. type User struct {
  2. UUID string `gorm:"primaryKey;unique;not null" json:"user_id"`
  3. Email string `gorm:"unique;not null" json:"email" binding:"required,email"`
  4. Password string `gorm:"-:all" json:"password"`
  5. DisplayName string `gorm:"unique;not null" json:"display_name" binding:"required"`
  6. FirstName string `gorm:"not null" json:"first_name" binding:"required"`
  7. LastName string `gorm:"not null" json:"last_name" binding:"required"`
  8. PhoneNumber string `gorm:"unique;not null" json:"phone_number" binding:"required"`
  9. DateOfBirth string `gorm:"not null" json:"date_of_birth" binding:"required"`
  10. EmailVerified bool `json:"email_verified"`
  11. PhoneVerified bool `json:"phone_verified"`
  12. PhotoURL string `json:"photo_url" binding:"omitempty,url"`
  13. UpdatedAt time.Time `json:"updated_at"`
  14. CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
  15. DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
  16. RefreshToken string `json:"refresh_token"`
  17. Friendship []Friendship `gorm:"many2many:users_friendships;" json:"-"`
  18. }

这是我的友谊模型:

  1. type Friendship struct {
  2. gorm.Model
  3. FromUserId string `gorm:"primaryKey" json:"from_user"`
  4. ToUserId string `gorm:"primaryKey" json:"to_user" binding:"required"`
  5. SentTime time.Time `json:"sent_time"`
  6. ResponseTime time.Time `json:"response_time"`
  7. Accepted bool `json:"accepted"`
  8. }

这是我的用户朋友模型:

  1. type UserFriend struct {
  2. UserUUID string
  3. FriendshipID uint
  4. Accepted bool
  5. }

这是我的迁移代码:

  1. func MigrateDB(db *gorm.DB) error {
  2. err := db.SetupJoinTable(&domain.User{}, "UserFriends", &domain.Friendship{})
  3. if err != nil {
  4. return err
  5. }
  6. err = db.AutoMigrate(&domain.User{}, &domain.Friendship{}, &joins.UserFriend{})
  7. if err != nil {
  8. return err
  9. }
  10. return nil
  11. }
英文:

Every time i run AutoMigrate() I get the error level=fatal msg="failed to found relation: UserFriends"

here is my user model

  1. type User struct {
  2. UUID string `gorm:"primaryKey;unique;not null" json:"user_id"`
  3. Email string `gorm:"unique;not null" json:"email" binding:"required,email"`
  4. Password string `gorm:"-:all" json:"password"`
  5. DisplayName string `gorm:"unique;not null" json:"display_name" binding:"required"`
  6. FirstName string `gorm:"not null" json:"first_name" binding:"required"`
  7. LastName string `gorm:"not null" json:"last_name" binding:"required"`
  8. PhoneNumber string `gorm:"unique;not null" json:"phone_number" binding:"required"`
  9. DateOfBirth string `gorm:"not null" json:"date_of_birth" binding:"required"`
  10. EmailVerified bool `json:"email_verified"`
  11. PhoneVerified bool `json:"phone_verified"`
  12. PhotoURL string `json:"photo_url" binding:"omitempty,url"`
  13. UpdatedAt time.Time `json:"updated_at"`
  14. CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
  15. DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
  16. RefreshToken string `json:"refresh_token"`
  17. Friendship []Friendship `gorm:"many2many:users_friendships;"`

}

here is my friendship model

  1. type Friendship struct {
  2. gorm.Model
  3. FromUserId string `gorm:"primaryKey" json:"from_user"`
  4. ToUserId string `gorm:"primaryKey" json:"to_user" binding:"required"`
  5. SentTime time.Time `json:"sent_time"`
  6. ResponseTime time.Time `json:"response_time"`
  7. Accepted bool `json:"accepted"`

}

here is my user friend model

  1. type UserFriend struct {
  2. UserUUID string
  3. FriendshipID uint
  4. Accepted bool

}

and my migration code

  1. func MigrateDB(db *gorm.DB) error {
  2. err := db.SetupJoinTable(&domain.User{}, "UserFriends", &domain.Friendship{})
  3. if err != nil {
  4. return err
  5. }
  6. err = db.AutoMigrate(&domain.User{}, &domain.Friendship{}, &joins.UserFriend{})
  7. if err != nil {
  8. return err
  9. }
  10. return nil

}

答案1

得分: 1

你应该在这里使用"自引用多对多"的关系。这里有一个很好的例子,展示了用户之间的好友关系。

英文:

You should use Self Referential Many to Many relationship here. Here is a nice example of user friend relationship.

huangapple
  • 本文由 发表于 2022年10月6日 11:48:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/73968486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定