使用Gorm时无法检索Has Many关联的问题

huangapple go评论126阅读模式
英文:

Trouble retrieving Has Many using Gorm

问题

当我尝试从播客中获取剧集时,我得到了invalid association []的错误。不确定我做错了什么。

  1. package main
  2. import (
  3. "log"
  4. "github.com/jinzhu/gorm"
  5. _ "github.com/mattn/go-sqlite3"
  6. )
  7. type Podcast struct {
  8. Id int
  9. Title string
  10. RssUrl string `sql:"unique_index"`
  11. Url string
  12. Episodes []Episode
  13. }
  14. type Episode struct {
  15. Id int
  16. PodcastID int `sql:"index"`
  17. Title string
  18. Url string `sql:"unique_index"`
  19. Downloaded bool
  20. }
  21. func main() {
  22. db, err := gorm.Open("sqlite3", "gorm.db")
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. db.LogMode(true)
  27. db.CreateTable(&Podcast{})
  28. db.CreateTable(&Episode{})
  29. podcast := Podcast{
  30. Title: "My Podcast",
  31. RssUrl: "http://example.com/feed/",
  32. Url: "http://www.example.com",
  33. Episodes: []Episode{{Title: "Episode One Point Oh!", Url: "http://www.example.com/one-point-oh", Downloaded: false}},
  34. }
  35. var episodes []Episode
  36. db.Model(&podcast).Related(&episodes)
  37. }
英文:

When I try to get episodes back from a podcast I get invalid association []. Not sure what I'm doing wrong.

  1. package main
  2. import (
  3. "log"
  4. "github.com/jinzhu/gorm"
  5. _ "github.com/mattn/go-sqlite3"
  6. )
  7. type Podcast struct {
  8. Id int
  9. Title string
  10. RssUrl string `sql:"unique_index"`
  11. Url string
  12. Episodes []Episode
  13. }
  14. type Episode struct {
  15. Id int
  16. PodcastID int `sql:"index"`
  17. Title string
  18. Url string `sql:"unique_index"`
  19. Downloaded bool
  20. }
  21. func main() {
  22. db, err := gorm.Open("sqlite3", "gorm.db")
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. db.LogMode(true)
  27. db.CreateTable(&Podcast{})
  28. db.CreateTable(&Episode{})
  29. podcast := Podcast{
  30. Title: "My Podcast",
  31. RssUrl: "http://example.com/feed/",
  32. Url: "http://www.example.com",
  33. Episodes: []Episode{{Title: "Episode One Point Oh!", Url: "http://www.example.com/one-point-oh", Downloaded: false}},
  34. }
  35. var episodes []Episode
  36. db.Model(&podcast).Related(&episodes)
  37. }

答案1

得分: 1

你正在使用的GO和GORM版本是哪个?我在我的机器上尝试了一下,这是日志:

[2015-06-17 19:02:11] [12.00ms] CREATE TABLE "podcasts" ("id" integer,"title" varchar(255),"rss_url" varchar(255),"url" varchar(255) , PRIMARY KEY ("id"))
[2015-06-17 19:02:11] [1.26ms] CREATE TABLE "episodes" ("id" integer,"podcast_id" integer,"title" varchar(255),"url" varchar(255),"downloaded" bool , PRIMARY KEY ("id"))
[2015-06-17 19:02:11] [1.25ms] SELECT * FROM "episodes" WHERE ("podcast_id" = '0')

请注意,因为您没有创建podcast变量,所以podcast_id为0,所以查询没有太多意义。

为了创建podcast,只需添加以下代码:

db.NewRecord(podcast)
db.Create(&podcast)

var episodes []Episode
db.Model(&podcast).Related(&episodes)

log.Print(episodes)

英文:

Which version of GO and GORM are you using? I've tried on my machine and this is the log:

  1. [2015-06-17 19:02:11] [12.00ms] CREATE TABLE "podcasts" ("id" integer,"title" varchar(255),"rss_url" varchar(255),"url" varchar(255) , PRIMARY KEY ("id"))
  2. [2015-06-17 19:02:11] [1.26ms] CREATE TABLE "episodes" ("id" integer,"podcast_id" integer,"title" varchar(255),"url" varchar(255),"downloaded" bool , PRIMARY KEY ("id"))
  3. [2015-06-17 19:02:11] [1.25ms] SELECT * FROM "episodes" WHERE ("podcast_id" = '0')

Please note that because you didn't create the podcast variable, the podcast_id is 0 so the query does not make so much sense.

In order to create the podcast, simply add this code

  1. db.NewRecord(podcast)
  2. db.Create(&podcast)
  3. var episodes []Episode
  4. db.Model(&podcast).Related(&episodes)
  5. log.Print(episodes)

huangapple
  • 本文由 发表于 2015年6月18日 00:20:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/30897123.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定