dpli36193 2015-06-21 20:29
浏览 258
已采纳

如何将记录插入到具有gorm的现有多个表中

I have two tables

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
}

I know how to insert episodes into a new podcast like so.

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)

How do I go about adding episodes to a podcast that already exists later on?

  • 写回答

1条回答 默认 最新

  • dqmgjp5930 2015-06-21 22:22
    关注

    I was able to figure it out.

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 搭建QEMU环境运行OP-TEE出现错误
  • ¥15 Minifilter文件保护
  • ¥15 onvif框架引用一直报错
  • ¥50 C#和C++混合编程,使用CLR托管,报错System.Runtime.InteropServices.SEHException
  • ¥30 .NET使用sqlite发布后报错
  • ¥15 微信小程序获取华为云影子设备失败,显示403报错
  • ¥15 Unity在WebGL平台导出Word报错问题
  • ¥15 ghpython这里总是报错而且rhino视图窗口内不显示怎么办
  • ¥15 布尔运算推导,请证明
  • ¥15 有没有人会打学生成绩管理系统呀