I'm starting with Gorm and trying to model the following:
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
}
As you can see I want to be able to refer to the same struct, MyLink
as both a foreign keyed object from MyPage
but also as a one-to-many from MyLinkSection
.
It seems I have to declare the foreign key ID in MyLink
which would seem to make this not possible.
Is there any way of setting up tables like this? With a normal DB I'd just have a field in MyPage
called my_link_id
, with something similar for MyLinkSection
.
It seems it is possible to specify forward relations:
PageUrl MyLink `gorm:"ForeignKey:PageUrlId"`
PageUrl Id uint