Gorm多重预压don';行不通
大家周一早上好! 我在预加载实体时遇到问题!我正在对实体进行多次预加载。Gorm多重预压don';行不通,go,go-gorm,Go,Go Gorm,大家周一早上好! 我在预加载实体时遇到问题!我正在对实体进行多次预加载。Country实体工作正常,但未预加载报告。知道是什么引起的吗 type Review struct { ID int Report Report 'gorm:"ForeignKey:ReportId"' ReportId uint Country Country 'gorm:"ForeignKey:CountryId"' CountryId
Country
实体工作正常,但未预加载报告。知道是什么引起的吗
type Review struct {
ID int
Report Report 'gorm:"ForeignKey:ReportId"'
ReportId uint
Country Country 'gorm:"ForeignKey:CountryId"'
CountryId uint
}
func FindApprovedReviews(page int, countryId int) ([]Review, int, error) {
var reviews [] Review
// Subtract one to fix Offset but keep page number correct
page -= 1
err := db.Where("approved_at IS NOT NULL").
//TODO: Change this to a configuration file value
Limit(10).
Where("approved_by IS NOT NULL").
Where("country_id = ?", countryId).
//TODO: Change this (the 10) to a configuration value
Offset(page * 10).
Order("id desc").
Preload("Report").
Preload("Country").
Find(&reviews).
Error
if err != nil {
return nil, 0, err
}
return reviews, count, err
}
两个相关结构(Country
和Report
)仅由gorm.Model
组成,用于测试
提前感谢您的帮助
编辑:我正在使用mysql
驱动程序
另外,[…]
表示已隐藏的业务数据,但它们是简单的列,工作正常。刚刚意识到我选择的查询与报表的关系(即FK)为空
如果关系键不为null,则可以正常工作