I try to setup a pretty simple association but can't manage it :
Type Books struct {
gorm.model
Name string
Tags []Tag `gorm:"many2many:books_tags_relations;association_autoupdate:true;association_autocreate:true;"`
}
Type Tag struct {
gorm.Model
Label string
Niveau string
}
the books share an amount of around 30 to 40 tags for now each book can have multiple tags each tag can be hooked to any number of books
for example the tag: "humour noir" can be only once in the table tags but used in 20 differents books.
if err := db.Debug().Model(&book).Association("Tags").Append(&tag); err != nil {
log.Errorf("[ERROR] erreur en sauvant les derniers tag %+v
", err)
}
{EDIT} :
if err := db.Debug().Model(&book).Association("Tags").Append(&tag).Error; err != nil {
log.Errorf("[ERROR] erreur en sauvant les derniers tag%v
", err)
}
no more error ... but I still have to understand why I got multiple time the same tag in the table ... {/EDIT}
and I got :
[ERROR] erreur en sauvant les derniers tag &{Error: scope:0xc42039ad00 column:Tag field:0xc42051cea0}
The other main problem is that multiple same tag are saved!
I want only one line pour the same tags, no need to have 1 per book/tag...
What am I doing wrong?
(Pretty sure, everything is in there but no way to get it out : https://github.com/jinzhu/gorm/blob/master/association_test.go)