我从Gorm开始并试图模拟以下内容:
type MyLink struct {
gorm.Model
Title string
Url string
}
// group of links under a single title
type MyLinkSection struct {
gorm.Model
Title string
Links []MyLink
}
type MyPage struct {
gorm.Model
PageUrl MyLink
Artists []MyLinkSection
}
正如您所看到的,我希望能够引用相同的结构, MyLink
作为 MyPage
中的外键控制对象,但也可以作为 MyLinkSection
中的一对多引用 .
似乎我必须在 MyLink
中声明外键ID,这似乎使得这不可能 .
有没有办法像这样设置表格?使用普通数据库,我只需在 MyPage
中有一个名为 my_link_id
的字段,类似于 MyLinkSection
.
1 回答
似乎可以指定前向关系: