我有两张 table
type Podcast struct {
Id int
Title string
RssUrl string `sql:"unique_index"`
Episodes []Episode
}
type Episode struct {
Id int
PodcastID int
Title string
Url string `sql:"unique_index"`
Downloaded bool
}
我知道如何像这样将剧集插入到新播客中。
podcast := Podcast{
Title: "My Podcast",
RssUrl: "http://example.com/feed/",
Url: "http://www.example.com",
Episodes: []Episode{{
Title: "Episode One Point Oh!",
Url: "http://www.example.com/one-point-oh",
Downloaded: false,
}},
}
db.Create(&podcast)
如何将剧集添加到以后已经存在的播客中?
最佳答案
我想通了。
var id int
row := db.Table("podcasts").Where("id = ?", 1).Select("id").Row()
row.Scan(&id)
episode := Episode{
Title: "Episode Two!",
Url: "http://www.example.com/episode-two",
Downloaded: true,
PodcastID: id,
}
db.Create(&episode)
关于go - 如何使用 gorm 将记录插入现有的有很多表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30969017/