I'm using Go with the GORM ORM. I have the following structs. The relation is simple. One Town has multiple Places and one Place belongs to one Town.
type Place struct {
ID int
Name string
Town Town
}
type Town struct {
ID int
Name string
}
Now i want to query all places and get along with all their fields the info of the corresponding town. This is my code:
db, _ := gorm.Open("sqlite3", "./data.db")
defer db.Close()
places := []Place{}
db.Find(&places)
fmt.Println(places)
My sample database has this data:
/* places table */
id name town_id
1 Place1 1
2 Place2 1
/* towns Table */
id name
1 Town1
2 Town2
i'm receiving this:
[{1 Place1 {0 }} {2 Mares Place2 {0 }}]
But i'm expecting to receive something like this (both places belongs to the same town):
[{1 Place1 {1 Town1}} {2 Mares Place2 {1 Town1}}]
How can i do such query ? I tried using Preloads
and Related
without success (probably the wrong way). I can't get working the expected result.
TownID
must be specified as the foreign key. ThePlace
struct gets like this:Now there are different approach to handle this. For example:
This will certainly produce the expected result, but notice the log output and the queries triggered.
The output is the expected but this approach has a fundamental flaw, notice that for every place there is the need to do another db query which produce a
n + 1
problem issue. This could solve the problem but will quickly gets out of control as the amount of places grow.It turns out that the good approach is fairly simple using preloads.
That's it, the query log produced is:
This approach will only trigger two queries, one for all places, and one for all towns that has places. This approach scales well regarding of the amount of places and towns (only two queries in all cases).
You do not specify the foreign key of towns in your Place struct. Simply add TownId to your Place struct and it should work.
To optimize query I use "in condition" in the same situation