Hi, I have the table “users”, “addresses”, and “hobbies”. Addresses and hobbies have a foreing key on users.
I want to retrieve a user with all his adresses and all his hobbies.
I could do this using three different select statements and joining the data in my go program, and filling a User struct that looks like this.
type User struct {
Name string
Addresses []Address
Hobbies []Hobbie
}
type Hobbie struct {
Name string
}
type Address struct {
Street string
City string
}
But there will be 3 roundtrips to the database which I’d like to avoid (in the real case there will be a lot more than 3).
Is there a way to do the whole query in one trip? Is there any recommended approach?
PD: I don’t want to use json in a string field, need the data in the other tables for integral referentiality.