gorm v1 中使用related时怎么带判断条件
在 GORM v1 中,可以通过在 Related 方法的参数中使用 Where 方法来添加条件。例如:
db.Model(&user).Related(&posts, "Posts").Where("status = ?", "published")
这将返回所有已发布的用户的帖子。在此示例中,user 是一个已知的用户实例,posts 是一个 []Post 类型的切片,"Posts" 是用户和帖子之间的关联关系。Where 方法用于添加条件,只返回符合条件的帖子。
原文地址: https://www.cveoy.top/t/topic/EjJ 著作权归作者所有。请勿转载和采集!