我正在尝试基于BookData
中的非主键字段检索作者的信息。下面你可以看到我的BookData
表中有AuthorId
,我试图根据AuthorId
获取作者,即使它不是主键。GORM似乎不支持这种类型的连接,有没有办法做到这一点?
您还可以在下面看到,我能够正确地获取PublisherProperty
信息,因为它的外键是BookData
的主键。我只是想知道如何做到这一点,如果它不是主键。先谢谢你了!
type BookData struct {
Id string `gorm:"primary_key;column:book_id"`
AuthorId string `gorm:"column:author_id"`
Author AuthorData `gorm:"foreignkey:AuthorId"`
PublisherProperty []PublisherProperty `gorm:"foreignkey:Id"`
}
type AuthorData struct {
Id string `gorm:"primary_key;column:author_id"`
Name string `gorm:"column:author_name"`
}
type PublisherProperty struct {
Id string `gorm:"primary_key;column:book_id"`
PublisherId string `gorm:"primary_key;column:publisher_id"`
PublisherTxt string `gorm:"column:publisher_txt"`
}
1条答案
按热度按时间o8x7eapl1#
您可以使用关键字
ForeignKey
和References
。有关更多用例,请参阅official documentation of GORM