Two structs Thread & Interest have a many to many relationship with each other. According to the documentation this is how both structs looks like:
type Thread struct {
ID uuid.UUID `gorm:"primaryKey" json:"threadId"`
Title string `gorm:"not null" json:"title"`
Body string `gorm:"not null" json:"body"`
CreatedAt time.Time
UpdatedAt time.Time
// Foreign keys
UserID uuid.UUID `json:"userId"`
// Has many association
Comments []Comment `json:"comments"`
// many to many associations
Interests []Interest `gorm:"many2many:thread_interests;" json:"interests"`
UsersLiked []User `gorm:"many2many:post_likes;" json:"usersLiked"`
UsersDisliked []User `gorm:"many2many:post_dislikes;" json:"usersDisliked"`
}
type Interest struct {
ID uuid.UUID `gorm:"primaryKey" json:"interestId"`
Name string `json:"name"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
// Many to many associations
UserProfiles []UserProfile `gorm:"many2many:user_interests;" json:"userProfiles"`
Threads []Thread `gorm:"many2many:thread_interests;" json:"threads"`
}
Since it is many to many between both structs I created another struct to act as join table named ThreadInterest. The code:
type ThreadInterest struct {
ThreadID uuid.UUID `gorm:"primaryKey;"`
InterestID uuid.UUID `gorm:"primaryKey;"`
CreatedAt time.Time
UpdatedAt time.Time
}
The problem is that whenever I create a new thread with the corresponding interests it creates the thread record but no new record is created in the join table ThreadInterest. I tried reading the documentation and searching online but I can't find a solution. I hope you guys could help me on this.