亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Gorm 檢索有很多問題

使用 Gorm 檢索有很多問題

Go
料青山看我應如是 2021-10-18 10:29:11
當我嘗試從播客中取回劇集時,我得到了invalid association []. 不知道我做錯了什么。package mainimport (    "log"    "github.com/jinzhu/gorm"    _ "github.com/mattn/go-sqlite3")type Podcast struct {    Id       int    Title    string    RssUrl   string `sql:"unique_index"`    Url      string    Episodes []Episode}type Episode struct {    Id         int    PodcastID  int `sql:"index"`    Title      string    Url        string `sql:"unique_index"`    Downloaded bool}func main() {    db, err := gorm.Open("sqlite3", "gorm.db")    if err != nil {        log.Fatal(err)    }    db.LogMode(true)    db.CreateTable(&Podcast{})    db.CreateTable(&Episode{})    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}},    }    var episodes []Episode    db.Model(&podcast).Related(&episodes)}
查看完整描述

1 回答

?
www說

TA貢獻1775條經驗 獲得超8個贊

您使用的是哪個版本的 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,因此查詢沒有多大意義。


為了創建播客,只需添加此代碼


db.NewRecord(podcast)

db.Create(&podcast)


var episodes []Episode

db.Model(&podcast).Related(&episodes)


log.Print(episodes)


查看完整回答
反對 回復 2021-10-18
  • 1 回答
  • 0 關注
  • 248 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號