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

问题描述 投票:0回答:2

我正在尝试使用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“在” website_ads“。” id“ = 2 LIMIT 50偏移量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
}




谁能帮忙吗?

go gorm go-gorm
2个回答
1
投票
 var websiteslots models.WebsiteSlots
    resp, _ := postgresConnection.Debug().Find(&websiteslots).Limit(limit).Offset(skip).Rows()

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


1
投票
websiteslots := []models.WebsiteSlots{}
postgresConnection.Debug().Limit(limit).Offset(skip).Find(&websiteslots)
return websiteslots 

[这里您使用Find()Row()都可能会出现问题。您在websiteslots中得到结果,然后为什么使用resp准备结果。

© www.soinside.com 2019 - 2024. All rights reserved.