How to only append valid associations?
Append-Associations
As described in association mode, we need to Append relations, but I'm getting a wrong use case... Since that I created 2 roles, but somehow gorm gives me 3. Take a look at the example:
user := User{ID: "test"}
db.Create(&user)
role := Role{ID: "role"}
role2 := Role{ID: "role2"}
role3 := Role{ID: "role3"}
// PAY ATTENTION HERE, I've only created role and role2
db.Create([]Role{role, role2})
fmt.Println(db.Model(&user).Association("Roles").Count()) // Should be zero
// How to don't create new role (role3), only append role and role2 here?
db.Model(&user).Association("Roles").Append([]Role{role, role2, role3})
fmt.Println(db.Model(&user).Association("Roles").Count()) // Gives me 3. Because role3 has been created automagic by gorm
As shown in the code I've only have created 2 roles, but when I look at roles table I see that are 3. And also 3 associations with the user even if role3 wasn't supposed to exist.
Expected answer
Append existing/valid association (role,role2) without automagic creating missing ones (role3).
Comment From: mirusky
Is kind of the same as #3605. Since that I'm closing it.