Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用gorm将记录插入现有has many表_Go_Go Gorm - Fatal编程技术网

如何使用gorm将记录插入现有has many表

如何使用gorm将记录插入现有has many表,go,go-gorm,Go,Go Gorm,我有两张桌子 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_

我有两张桌子

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)