I have two sets objects, which are pretty similar but not exactly. Generated by protobuf:
// generated by protobuf
type User struct {
Id string
Name string
Group []string
}
type Group struct {
Id string
Name string
}
There is also Gorm types, which are similar but not exactly:
// gorm storage definition
type User struct {
ID string `gorm:"primary_key;size:36;"`
Name string
Groups []*Group `gorm:"many2many:user_groups;"`
}
type Group struct {
ID string `gorm:"primary_key;size:36"`
Name string
Users []*User `gorm:"many2many:user_groups;"`
}
I would like to execute something like:
var users []*pb.User
s.db.Preload("Groups").Find(&users).Related("Groups")
and get protobuf users populated with User objects, and group ids. Unfortunatelly I get memory address string like so: [0xc0001de460 0xc0001de5a0]
.
The naive way is to do a loop over users and then append all the group ids, and create pb.User object with it. I do not like it, and it may be slow.
Considering that many2many
doesn't store groups in users
table at all, there should be a way to retrieve group ids and include them in the result from user_groups
table.
But I can't fine the solution in the documentation, except maybe doing almost everything manually (SQL query, Scan, etc).
am I missing something or I need something extraordinary?