2

I am wondering how the correct approach is to load related fields in beego. The doc explains it like this:

type User struct {
   Id    int
   Name  string
   Posts []*Post `orm:"reverse(many)"`
}

user := User{Id: 1}
err := dORM.Read(&user)
num, err := dORM.LoadRelated(&user, "Posts")

This makes sense as long as I only query one record. What is the correct way to fetch related fields when I query all users?

A possible solution would be like this:

var users []*User
o.QueryTable(new(User)).All(&users)
for _, user := range users {
    o.LoadRelated(controlCategory, "Posts")
}

However, this means I have to loop everytime over the complete list and make for every record a DB query to load all records.

Any suggestions? Thanks!

Max
  • 21
  • 3
  • Your possible solution is the way to do it in beego's orm: https://github.com/astaxie/beego/issues/1258 – mkopriva Apr 19 '17 at 15:04
  • @mkopriva Thank you. I saw this post too. Since this post is 2 years old, I was hoping there is a new approach. The working approach is very bad performance wise. – Max Apr 20 '17 at 06:44
  • Hi Max - Did you find a solution to this please,thank you. – user1619524 Jul 19 '18 at 08:45

0 Answers0