我正在使用Gorm和Golang从我的数据库中获取数据。有没有可能让 Gorm 也获取对象子项(外键)?数据库表users+----+---------+------------+| id | name | country_id |+----+---------+------------+| 1 | Adam | 1 || 2 | Bertil | 1 || 3 | Charlie | 2 |+----+---------+------------+countries+----+--------+| id | name |+----+--------+| 1 | Sweden || 2 | Norway |+----+--------+模型type User struct { Id int64 `json:"-"` Name string `json:"name"` CountryId int64 `json:"-"` Country Country `json:"country"`}type Country struct { Id int64 `json:"-"` Name string `json:"name"`}用于获取所有用户的代码var users []UserDB.Find(&users) // Question: How should this be modified to automatically fetch the Country?实际结果[ { "name": "Adam", "country" : { "name": "", } }, ...]期望的结果[ { "name": "Adam", "country" : { "name": "Sweden", } }, ...]非常感谢您的输入!/克拉雷
- 1 回答
- 0 关注
- 85 浏览
添加回答
举报
0/150
提交
取消