在这里,我尝试加入 Branches_organization、orgnization 和 locations 这三个表,并将它们作为 API 响应发送。但是,organizationand location json 无法获取任何数据。
问题:
以下API响应无法检索组织和位置详细信息;总是返回零值
{
"Title": "OK",
"status": 200,
"records": 1,
"data": [
{
"branch_id": 51,
"name": "my branch name",
"address": "Mun",
"telephone": "+9xxxxx",
"organization": {
"org_id": 0,
"name": ""
},
"location": {
"loc_id": 0,
"name": ""
}
}
]
}
控制台错误
2022/08/17 06:11:32 /apiserver/controllers/handlers.go:3475 invalid field found for struct apiserver/entities.Custom_branches_organization's field Organization: define a valid foreign key for relations or implement the Valuer/Scanner interface
[7.930ms] [rows:1] SELECT branches_organization.branch_id,branches_organization.name,branches_organization.address,branches_organization.telephone,organization.org_id,organization.name,location.loc_id,location.name FROM `branches_organization` left join organization on organization.org_id = branches_organization.org_id left join location on location.loc_id = branches_organization.loc_id
实施细节
使用的结构
用于 API 响应的结构
type Custom_branches_organization struct {
Branch_id uint `json:"branch_id"`
Name string `json:"name" validate:"required"`
Address string `json:"address" validate:"required"`
Telephone string `json:"telephone" validate:"e164,required"`
Organization Organization `json:"organization" gorm:"foreignkey:org_id;references:Org_id"`
Location Location `json:"location" gorm:"foreignkey:loc_id;references:Loc_id"`
}
子结构 //数据库中的组织表
type Organization struct {
Org_id uint `json:"org_id"`
Name string `json:"name" validate:"required"`
Address string `json:"address"`
Telephone string `json:"telephone" validate:"e164,required"`
Email string `json:"email" validate:"required,email"`
}
//数据库中的位置表
type Location struct {
Loc_id uint `json:"loc_id"`
Name string `json:"name" validate:"required"`
Address string `json:"address" validate:"required"`
Telephone string `json:"telephone" validate:"e164,required"`
}
//数据库中的实际分支表
type Branches_organization struct {
Branch_id uint `json:"branch_id"`
Name string `json:"name" validate:"required"`
Address string `json:"address" validate:"required"`
Telephone string `json:"telephone" validate:"e164,required"`
Org_id uint `json:"org_id" validate:"number"`
Loc_id uint `json:"loc_id" validate:"number"`
}
Gorm 实施
//从branches_organization表中取出所有记录
var branches_organizations []entities.Custom_branches_organization
result := database.Instance.Model(&entities.Branches_organization{}).Preload("Organization").Preload("Location").Select("branches_organization.branch_id,branches_organization.name,branches_organization.address,branches_organization.telephone,organization.org_id,organization.name,location.loc_id,location.name").Joins("left join organization on organization.org_id = branches_organization.org_id").Joins("left join location on location.loc_id = branches_organization.loc_id").Scan(&branches_organizations)
branches_organizationsdata := entities.Customebranches_organizationData{"OK", http.StatusOK, result.RowsAffected, branches_organizations}
json.NewEncoder(w).Encode(branches_organizationsdata)
数据库表
mysql> desc organization;
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| org_id | int | NO | PRI | NULL | auto_increment |
| name | longtext | YES | | NULL | |
| address | longtext | YES | | NULL | |
| telephone | longtext | YES | | NULL | |
| email | longtext | YES | | NULL | |
+-----------+----------+------+-----+---------+----------------+
5 rows in set (0.12 sec)
mysql> desc location;
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| loc_id | int | NO | PRI | NULL | auto_increment |
| name | longtext | YES | | NULL | |
| address | longtext | YES | | NULL | |
| telephone | longtext | YES | | NULL | |
+-----------+----------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> desc branches_organization;
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| branch_id | int | NO | PRI | NULL | auto_increment |
| name | longtext | YES | | NULL | |
| address | longtext | YES | | NULL | |
| telephone | longtext | YES | | NULL | |
| org_id | int | YES | MUL | NULL | |
| loc_id | int | YES | MUL | NULL | |
+-----------+----------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
我花了很多天的时间来创建一个 json 回复,它将组织和位置详细信息作为嵌套结构。希望大家多多支持
你必须使用预加载来填充嵌套结构,类似于在 sql 查询中加入。请参考此文档。
https://gorm.io/docs/preload.html#nested_preloading
type User struct {
gorm.Model
Username string
Orders Order
}
type Order struct {
gorm.Model
UserID uint
Price float64
}
db.Preload("Username")
db.Preload("Orders").Find(&users)