我正在嘗試使用 Postgres 連接與 golang 中的 gorm 查詢并獲取所有數據。我的戈爾姆模型 type WebsiteSlots struct { Id uint `gorm:"primary_key"` Settings string `gorm:"json"` AdsizeId int `gorm:"type:int"` WebsiteId int `gorm:"type:int"` AdSize Adsizes `gorm:"foreignkey:AdSizesId"` Website Websites `gorm:"foreignkey:WebsiteId"` UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP"` CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP"`}func (WebsiteSlots) TableName() string { return "website_ads"}我的查詢存儲庫 GetSlots() 正在生成“從“website_ads”中選擇*,其中“website_ads”。“id”= 2 LIMIT 50 OFFSET 0”這個查詢。我不知道這個“”website_ads”.“id”= 2”是怎么來的?type Slots struct { Container *container.Container}func (w *Slots) GetSlots(skip int , limit int) []models.WebsiteSlots { var results []models.WebsiteSlots sites := w.Container.Get("dbprovider").(*services.Database) postgresConnection := sites.PostgresConnection() postgresConnection.LogMode(true) var websiteslots models.WebsiteSlots resp, _ := postgresConnection.Debug().Find(&websiteslots).Limit(limit).Offset(skip).Rows() //i := 0 for resp.Next() { results = append(results,websiteslots) } return results}有人可以幫忙嗎?
2 回答

墨色風雨
TA貢獻1853條經驗 獲得超6個贊
var websiteslots models.WebsiteSlots resp, _ := postgresConnection.Debug().Find(&websiteslots).Limit(limit).Offset(skip).Rows()
Find() 需要一個結構體切片。但您提供的是單個結構。我不知道這是否是額外 WHERE 子句的原因。

慕俠2389804
TA貢獻1719條經驗 獲得超6個贊
websiteslots := []models.WebsiteSlots{}
postgresConnection.Debug().Limit(limit).Offset(skip).Find(&websiteslots)
return websiteslots
在這里你使用Find()&Row()這兩者都可能會出現問題。你得到結果websiteslots然后為什么要使用resp準備結果。
- 2 回答
- 0 關注
- 230 瀏覽
添加回答
舉報
0/150
提交
取消