Gorm 添加了我不需要的不需要的 where 子句

我正在尝试使用 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

}





有人可以帮忙吗?


米琪卡哇伊
浏览 132回答 2
2回答

墨色风雨

var websiteslots models.WebsiteSlots     resp, _ := postgresConnection.Debug().Find(&websiteslots).Limit(limit).Offset(skip).Rows()Find() 需要一个结构体切片。但您提供的是单个结构。我不知道这是否是额外 WHERE 子句的原因。

慕侠2389804

websiteslots := []models.WebsiteSlots{}postgresConnection.Debug().Limit(limit).Offset(skip).Find(&websiteslots)return websiteslots 在这里你使用Find()&Row()这两者都可能会出现问题。你得到结果websiteslots然后为什么要使用resp准备结果。
打开App,查看更多内容
随时随地看视频慕课网APP