多对多关系返回空值

I want to create many to many relation using beego. This is my struct:

type User struct {
    Id         int
    UserName   string `orm:"unique"`
    Department string
    Email      string
    Groups     []*Group `orm:"rel(m2m)"` 
}

type Group struct {
    Id        int
    GroupName string `orm:"unique"`
    Introduce string
    Users     []*User `orm:"reverse(many)"`
}

How do I relate between these structures.

o := orm.NewOrm()
u1 := User{UserName: "zhangszan"}
u2 := User{UserName: "lisi"}
g1 := Group{GroupName: "g1"}
g2 := Group{GroupName: "g2"}

o.InsertMulti(2, []User{u1, u2})
o.InsertMulti(2, []Group{g1, g2})

m2mu1 := o.QueryM2M(&u1, "Groups")
m2mu2 := o.QueryM2M(&u2, "Groups")

m2mu1.Add(&g1)
m2mu1.Add(&g2)

m2mu2.Add(&g2)
beego.Debug(u1)
beego.Debug(u2)

The console prints two empty arrays. I think there should be values in these two arrays. Does anyone know how to solve this problem? thank.

 {0 zhangszan   []}
 {0 lisi   []}

Environment

  • Beego : 1.11.1
  • GoVersion : go1.10
  • GOOS : windows
  • GOARCH : amd64