我使用GORM在Golang中构建模型关联,我有一个名为 Category 的结构 . 一个类别可以有许多子类别,它可能有一个父类别:
type Category struct {
Name string `json:"name"`
Parent Category `json:"parent_category"`
ParentGroupID uint `json:"parent_group_id"`
Children []Category `json:"children_categories"`
}
对于这个结构,我收到 Invalid recursive type Category 的错误 . 我检查了GORM文档,但没有找到任何帮助 . 有关如何使用GORM模拟这种关系的任何想法?
1 回答
您必须将
Parent
声明为*Category
(指向Category
的指针)而不是Category
.编译器如何知道
Parent
的大小 . 指针的大小是已知的,但包含自身的东西有多大? (并且内部结构也包含自身,内部结构也是如此,等等 . )参考:https://stackoverflow.com/a/8261789/4794989