Many To Many select
Hi. I have second entities:
type Note struct {
ID uuid.UUID `gorm:"primaryKey;type:uuid"`
Category *Category
Labels []*Label `gorm:"many2many:note_labels;"`
}
type Category struct {
ID uuid.UUID `gorm:"primaryKey;type:uuid"`
Name string `gorm:"type:varchar(40);not null"`
Labels []*Label // one category has N labels
}
type Label struct {
ID uuid.UUID `gorm:"primaryKey;type:uuid"`
Name string `gorm:"type:varchar(40);not null"`
CategoryID uuid.UUID
Category *Category
}
What i want? Select all notes by some condition and preload Note.Labels.
In code it's looks like:
var noteList []*model.Note
scopeDTO := transformToScopeDTO(filterDTO)
if err := r.db.Debug().WithContext(ctx).
Preload("Category").
Preload("Labels").
Scopes(scopeDTO...).
Where("deleted_at IS NULL").
Order("created_at desc").
Limit(limit).
Offset(offset).
Find(¬eList).Error; err != nil {
return nil, errors.WithStack(err)
}
return noteList, nil
Debug returns next sql queries:
2020/10/17 13:28:13 .../module/note/repository/note.go:94
[1.086ms] [rows:1] SELECT * FROM "notes_categories" WHERE "notes_categories"."id" = 'a2186370-d549-4923-b2a0-c759dceecf89'
2020/10/17 13:28:13 ../module/note/repository/note.go:94
[1.142ms] [rows:1] SELECT * FROM "note_labels" WHERE "note_labels"."note_id" = '6c40f4fb-d698-4a90-b194-e1b2d288ca91'
2020/10/17 13:28:13 .../module/note/repository/note.go:94
[1.309ms] [rows:0] SELECT * FROM "note_category_labels" WHERE "note_category_labels"."id" = 'eca71c21-30bf-4a63-bdf8-1022bafd6f4b'
2020/10/17 13:28:13 .../module/note/repository/note.go:94
[5.104ms] [rows:1] SELECT * FROM "notes" WHERE player_id = 'c8a3c60f-5a8d-438d-84eb-1a30b5e09fdf' AND (category_id = 'a2186370-d549-4923-b2a0-c759dceecf89') AND (is_priority = true) AND created_at >= '2020-01-01 00:00:00' AND created_at <= '2020-01-02 23:59:59.999' AND deleted_at IS NULL ORDER BY created_at desc LIMIT 10
But when I iterate over noteList variable, Labels are nil.
How I can fix it? Thanks.
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: voodoo-dn
@jinzhu check my question, please
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
Comment From: allidoisace
@voodoo-dn May be because Labels should be Label?