GORM关联查询使用请教
1. 运行环境
[版本]: go version go1.18 windows/amd64
2. 问题描述?
Go语言使用GORM:
type Article struct {
ArticleScore ArticleScore `json:"article_score"`
}
type ArticleScore struct {
}
global.GlobalDb.Where("id = ?", id).Preload("ArticleScore").First(&art)
通过查询article表能preload出articlescore表相关的数据,反过来查articlescore表出不来article表的数据,请问反过来的话得怎么使用呀,请大神指点,谢谢
因为你只在
Article
结构体里配置了ArticleScore
,没有在ArticleScore
结构体里配置Article
,加上配置就行了我实测是可以的
model:
repositorie:
run:
print:
@renxiaotu Preload中的查询条件不能影响整体结果,如果主表有数据 子表没数据,就不被查出来,现在查完了如果没数据是 只有关联字段是空的 这怎么处理呢