drq1257 2019-07-13 03:43 采纳率: 0%
浏览 60

表中只有一个字段的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?

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器