How to create a record? many2many Gorm

Hello!

I want to know how can I create a record in a many2many relationship.

Say, I have an user and I want to add a new language to him or viceversa, I have a language a would like to add a new user to it. How could I do it?

type User struct {
    gorm.Model
    Languages         []Language `gorm:"many2many:user_languages;"`
}

type Language struct {
    gorm.Model
    Name string
    Users               []User     `gorm:"many2many:user_languages;"`
}

I know I can create a record from scratch, but how would I append a new one?

Have you tried what is described in Gorm documents? Look for append: http://jinzhu.me/gorm/associations.html#association-mode

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.