Your Question
数据库数据: MariaDB [svcuser]> select * from article_replies; +----+-------------------------+-------------------------+------------+-----------+---------------+----------+------+------------+ | id | created_at | updated_at | deleted_at | author_id | content | upper_id | type | article_id | +----+-------------------------+-------------------------+------------+-----------+---------------+----------+------+------------+ | 1 | 2022-03-08 11:30:17.111 | 2022-03-08 11:30:17.111 | NULL | 1 | 评论1 | 0 | 1000 | 1 | | 2 | 2022-03-08 11:31:02.203 | 2022-03-08 11:31:02.203 | NULL | 1 | 评论2 | 0 | 1000 | 1 | | 3 | 2022-03-08 11:31:37.657 | 2022-03-08 11:31:37.657 | NULL | 1 | 回复评论2 | 2 | 1000 | 1 | | 4 | 2022-03-08 15:26:57.344 | 2022-03-08 15:26:57.344 | NULL | 1 | 回复评论3 | 3 | 1000 | 1 | | 5 | 2022-03-08 17:46:37.510 | 2022-03-08 17:46:37.510 | NULL | 1 | 点赞1 | 0 | 1001 | 1 | +----+-------------------------+-------------------------+------------+-----------+---------------+----------+------+------------+
type ArticleReply struct {
gorm.Model
... 其他字段...
UpperID *uint gorm:"comment:'上条ID'"
Children []User gorm:"foreignkey:UpperID"
}
func main() { docs := []ArticleReply{} DB.Debug().Where("upper_id = ?",0).Preload(clause.Associations).Find(&docs) fmt.Printf("%+v",docs) }
上面的代码无法查出id=3的Children 通过增加Preload("Children.Children")才能查出下一级。
The document you expected this should be explained
如果有下一级如何实现自动预加载下一级。
Expected answer
Comment From: weidongbcn
好像没有这样的功能吧。大多数都是得到数据再自己分类组合。
Comment From: baabaaox
see https://github.com/go-gorm/gorm/pull/4172
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days