【发布时间】:2021-04-07 02:06:40
【问题描述】:
我正在使用 gorm 处理数据库查询,我有 2 个模型 (ManyToMany):
type Person struct {
ID uint `json:"id" gorm:"primary_key;unique;autoIncrement"`
Name string `json:"name" binding:"required"`
Family string `json:"family" binding:"required"`
Companies []Company `json:"companies" gorm:"many2many:person_companies;"`
}
type Company struct {
ID uint `json:"id" gorm:"primary_key;unique;autoIncrement"`
Name string `json:"name"`
cars []Car
}
我使用此查询接收我的用户列表:
func GetAllPeople() *[]domain.Person {
var people []domain.Person
db.Find(&people)
return &people
}
这可行,但显示公司为 Null
{
"id": 0,
"name": "erfan",
"family": "",
"companies": null
}
我应该在查询中使用什么来在列表中显示用户公司 (id)?
【问题讨论】: