为了账号安全,请及时绑定邮箱和手机立即绑定

Gorm 正在添加我不需要的不需要的 where 子句

Gorm 正在添加我不需要的不需要的 where 子句

Go
哔哔one 2022-05-23 16:10:14
我正在尝试使用 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() 正在生成"SELECT * FROM "website_ads" WHERE "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 回答

?
慕容708150

TA贡献1831条经验 获得超4个赞

var websiteslots models.WebsiteSlots
    resp, _ := postgresConnection.Debug().Find(&websiteslots).Limit(limit).Offset(skip).Rows()

Find() 需要一个结构切片。但是您提供的是单个结构。我不知道这是否是额外 WHERE 子句的原因。


查看完整回答
反对 回复 2022-05-23
?
ITMISS

TA贡献1871条经验 获得超8个赞

websiteslots := []models.WebsiteSlots{}

postgresConnection.Debug().Limit(limit).Offset(skip).Find(&websiteslots)

return websiteslots 

在这里你使用Find()&Row()两者都可能会出现问题。你得到结果websiteslots然后为什么要使用resp准备结果。


查看完整回答
反对 回复 2022-05-23
  • 2 回答
  • 0 关注
  • 146 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信