#GORM v2 Many to Many does not create new record in join table

2 messages · Page 1 of 1 (latest)

upbeat vessel
#

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.

#

UPDATE: Here is how I create the thread

handler:

func HandleCreateThread(context *gin.Context) {

    var threadInput apimodels.ThreadInput

    err := context.ShouldBindJSON(&threadInput)

    if err != nil {
        // return error
        context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    userId, err := api.GetTokenUserId(context)

    if err != nil {
        // return error
        context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    var interests []databasemodels.Interest

    interests, err = dataaccess.FindInterestByIds(threadInput.Interests)

    if err != nil {
        context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    thread := databasemodels.Thread{
        Title:     threadInput.Title,
        Body:      threadInput.Body,
        Interests: interests,
        UserID:    userId,
    }

    newThread, err := dataaccess.CreateNewThread(&thread)

    if err != nil {
        context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }

    context.JSON(http.StatusOK, gin.H{"thread": newThread})
}