I would like my Gorm query to return the correct version_count. My Gorm query is returning 0 version_count, while my MySQL query returns the version_count.
I have the MySQL query below for the tables that left joins the subquery count into the final query results.
I have this for my Gorm query:
subQuery := models.DB.Select("services.id as services_id, COUNT(versions.service_id) as version_count").Group("services.id").Preload("Versions").Table("Services").SubQuery()
models.DB.Preload("Versions").Find(&services).Count(&count).Limit(limit).Offset(offset).
Joins("LEFT JOIN (?) as version_counts ON version_counts.services_id = s.id", subQuery)
go structs
type Service struct {
ID uint `json:"id" gorm:"primary_key"`
Name string `json:"name"`
Description string `json:"description"`
Versions []Version `json:"versions"`
VersionCount int `json:"version_count"`
CreatedAt string `json:"created_at"`
}
type Version struct {
ID uint `json:"id" gorm:"primary_key"`
Version string `json:"name"`
ServiceID string `json:"service_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
There are my tables with the queries.
mysql> select * from services;
+----+---------------------+---------------------+------------+--------+-------------+
| id | created_at | updated_at | deleted_at | name | description |
+----+---------------------+---------------------+------------+--------+-------------+
| 1 | 2021-11-05 21:16:25 | 2021-11-05 21:16:25 | NULL | John | New York |
| 2 | 2021-11-05 21:16:25 | 2021-11-05 21:16:25 | NULL | Martin | Los Angeles |
+----+---------------------+---------------------+------------+--------+-------------+
2 rows in set (0.02 sec)
mysql> select * from versions;
+----+---------------------+---------------------+------------+------------+---------+
| id | created_at | updated_at | deleted_at | service_id | version |
+----+---------------------+---------------------+------------+------------+---------+
| 1 | 2021-11-05 21:16:25 | 2021-11-05 21:16:25 | NULL | 1 | 1.0.0 |
| 2 | 2021-11-05 21:16:25 | 2021-11-05 21:16:25 | NULL | 1 | 1.0.1 |
| 3 | 2021-11-05 21:16:25 | 2021-11-05 21:16:25 | NULL | 1 | 1.0.2 |
| 4 | 2021-11-05 21:16:25 | 2021-11-05 21:16:25 | NULL | 2 | 1.0.0 |
| 5 | 2021-11-05 21:16:25 | 2021-11-05 21:16:25 | NULL | 2 | 1.0.1 |
| 6 | 2021-11-05 21:16:25 | 2021-11-05 21:16:25 | NULL | 2 | 1.0.2 |
| 7 | 2021-11-05 21:16:25 | 2021-11-05 21:16:25 | NULL | 2 | 1.0.3 |
+----+---------------------+---------------------+------------+------------+---------+
7 rows in set (0.01 sec)
mysql> select * from services s join versions v on v.service_id = s.id left join ( select s.id services_id, count(v.service_id) as version_count from services s join versions v on v.service_id = s.id group by s.id) version_counts ON version_counts.services_id = s.id;
+----+---------------------+---------------------+------------+--------+-------------+----+---------------------+---------------------+------------+------------+---------+-------------+---------------+
| id | created_at | updated_at | deleted_at | name | description | id | created_at | updated_at | deleted_at | service_id | version | services_id | version_count |
+----+---------------------+---------------------+------------+--------+-------------+----+---------------------+---------------------+------------+------------+---------+-------------+---------------+
| 1 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | John | New York | 1 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | 1 | 1.0.0 | 1 | 3 |
| 1 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | John | New York | 2 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | 1 | 1.0.1 | 1 | 3 |
| 1 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | John | New York | 3 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | 1 | 1.0.2 | 1 | 3 |
| 2 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | Martin | Los Angeles | 4 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | 2 | 1.0.0 | 2 | 4 |
| 2 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | Martin | Los Angeles | 5 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | 2 | 1.0.1 | 2 | 4 |
| 2 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | Martin | Los Angeles | 6 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | 2 | 1.0.2 | 2 | 4 |
| 2 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | Martin | Los Angeles | 7 | 2021-11-05 20:20:09 | 2021-11-05 20:20:09 | NULL | 2 | 1.0.3 | 2 | 4 |
+----+---------------------+---------------------+------------+--------+-------------+----+---------------------+---------------------+------------+------------+---------+-------------+---------------+
7 rows in set (0.01 sec)
I was able to load the response with the appropriate version_count.
models.DB.Preload("Versions").Table("Services s").
Select("*").
Joins("left join (select s.id services_id, count(v.service_id) as version_count " +
"from services s " +
"join versions v on v.service_id = s.id " +
"group by s.id) version_counts " +
"ON version_counts.services_id = s.id").
Find(&services)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.