英文:
How to join multiple tables using GORM without Preload
问题
我目前有3个通过GORM关联的表格。我想查询关于关联的所有卖家的信息。以下是我的实体:
type ShopType struct {
ID uint `gorm:"primarykey" json:"id"`
Name string `json:"name" xml:"name" form:"name" query:"name"`
}
type Shop struct {
ID uint `gorm:"primarykey" json:"id"`
Name string `json:"name" xml:"name" form:"name" query:"name"`
ShopType ShopType `gorm:"ShopTypeID;constraint:OnUpdate:CASCADE,OnDelete:RESTRICT;" json:"-"`
}
type Seller struct {
ID uint `gorm:"primarykey" json:"id"`
Firstname string `json:"firstname" xml:"firstname" form:"firstname" query:"firstname"`
Lastname string `json:"lastname" xml:"lastname" form:"lastname" query:"lastname"`
Shop Shop `gorm:"foreignKey:ShopID;constraint:OnUpdate:CASCADE,OnDelete:RESTRICT;" json:"-"`
}
是否可以使用Joins
而不是Preload
来实现,例如:
db.Model(&models.Seller{}).Joins("Shop").Joins("Shop.ShopType").Find(&results)
?
我尝试过这个方法,但它不起作用。
我还尝试过:
db.Model(&models.Seller{}).Joins("JOIN shops s on s.id = sellers.shop_id").Joins("JOIN shop_types st on st.id = s.shop_type_id")
这个方法可以工作,但它只填充了卖家的信息,而没有填充Shop和ShopType实体的属性。
我想使用Joins
而不是Preload
来连接我的实体,因为我想在查询中添加一些条件,例如:.Where('Shop.ShopType.Name IN (?)')
,而使用Preload
方法是不可能的。
英文:
I currently have 3 tables with relation between them through GORM. I'm looking to query the sellers with all informations about the relation.
Here's my entities:
type ShopType struct {
ID uint `gorm:"primarykey" json:"id"`
Name string `json:"name" xml:"name" form:"name" query:"name"
}
type Shop struct {
ID uint `gorm:"primarykey" json:"id"`
Name string `json:"name" xml:"name" form:"name" query:"name"
ShopType ShopType `gorm:"ShopTypeID;constraint:OnUpdate:CASCADE,OnDelete:RESTRICT;" json:"-"`
}
type Seller struct {
ID uint `gorm:"primarykey" json:"id"`
Firstname string `json:"firstname" xml:"firstname" form:"firstname" query:"firstname"
Lastname string `json:"lastname" xml:"lastname" form:"lastname" query:"lastname"
Shop Shop `gorm:"foreignKey:ShopID;constraint:OnUpdate:CASCADE,OnDelete:RESTRICT;" json:"-"`
}
It's not possible to use Joins
instead of Preload
like :
db.Model(&models.Seller{}).Joins("Shop").Joins("Shop.ShopType").Find(&results)
?
I have tried this but it doesn't work.
Also I have tried :
db.Model(&models.Seller{}).Joins("JOIN shops s on s.id = sellers.shop_id").Joins("JOIN shop_types st on st.id = s.shop_type_id")
It's work but it didn't fill the props of the Shop and ShopType entities, only the informations about the sellers are filled.
I'm looking to joins my entities using Joins
instead of Preload
because I want add some clauses to my query like : .Where('Shop.ShopType.Name IN (?)')
and that's not possible with the Preload
method.
答案1
得分: 2
你离第二个查询很接近了,你可以将Preload
和Where
函数与其结合使用。
var sellers []Seller
db.Joins("JOIN shops s on s.id = sellers.shop_id").
Joins("JOIN shop_types st on st.id = s.shop_type_id").
Preload("Shop.ShopType").
Where("st.name IN (?)", []string{"Store1", "Store2"}).
Find(&sellers)
请注意,这是一个Go语言的代码示例。
英文:
You are almost there with the second query, you can combine the Preload
and Where
functions with it.
var sellers []Seller
db.Joins("JOIN shops s on s.id = sellers.shop_id").
Joins("JOIN shop_types st on st.id = s.shop_type_id").
Preload("Shop.ShopType").
Where("st.name IN (?)", []string{"Store1", "Store2"}).
Find(&sellers)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论