【发布时间】:2020-05-16 23:30:13
【问题描述】:
我在GO 中有一个struct 定义,就像这样
package models
//StoryStatus indicates the current state of the story
type StoryStatus string
const (
//Progress indicates a story is currenty being written
Progress StoryStatus = "progress"
//Completed indicates a story was completed
Completed StoryStatus = "completed"
)
//Story holds detials of story
type Story struct {
ID int
Title string `gorm:"type:varchar(100);unique_index"`
Status StoryStatus `sql:"type ENUM('progress', 'completed');default:'progress'"`
Paragraphs []Paragraph `gorm:"ForeignKey:StoryID"`
}
//Paragraph is linked to a story
//A story can have around configurable paragraph
type Paragraph struct {
ID int
StoryID int
Sentences []Sentence `gorm:"ForeignKey:ParagraphID"`
}
//Sentence are linked to paragraph
//A paragraph can have around configurable paragraphs
type Sentence struct {
ID uint
Value string
Status bool
ParagraphID uint
}
我在 GO 中使用 GORM 作为 orm。
如何基于story id 获取story 的所有信息,例如所有paragraphs 和每个sentences 的所有sentences。
GORM 示例仅显示使用 preload 的 2 个模型
【问题讨论】:
标签: mysql go one-to-many go-gorm