Gorm 正在添加我不需要的不需要的 where 子句
Gorm is adding unwanted where clause that i don't need
我正在尝试使用 Postgres 与 golang 中的 gorm 连接来查询和获取所有数据。
我的 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
}
有人可以帮忙吗?
var websiteslots models.WebsiteSlots
resp, _ := postgresConnection.Debug().Find(&websiteslots).Limit(limit).Offset(skip).Rows()
Find() 需要一片结构。但是您提供的是单个结构。我不知道这是否是额外的 WHERE 子句的原因。
websiteslots := []models.WebsiteSlots{}
postgresConnection.Debug().Limit(limit).Offset(skip).Find(&websiteslots)
return websiteslots
这里使用Find()
& Row()
都可能出现问题。您在 websiteslots
中得到结果,那么为什么要使用 resp
来准备结果。
我正在尝试使用 Postgres 与 golang 中的 gorm 连接来查询和获取所有数据。
我的 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
}
有人可以帮忙吗?
var websiteslots models.WebsiteSlots
resp, _ := postgresConnection.Debug().Find(&websiteslots).Limit(limit).Offset(skip).Rows()
Find() 需要一片结构。但是您提供的是单个结构。我不知道这是否是额外的 WHERE 子句的原因。
websiteslots := []models.WebsiteSlots{}
postgresConnection.Debug().Limit(limit).Offset(skip).Find(&websiteslots)
return websiteslots
这里使用Find()
& Row()
都可能出现问题。您在 websiteslots
中得到结果,那么为什么要使用 resp
来准备结果。