表中只有一个字段的Many2many关系预加载

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?

orm
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐