How to join multiple tables using GORM without Preload

huangapple go评论77阅读模式
英文:

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

你离第二个查询很接近了,你可以将PreloadWhere函数与其结合使用。

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)

huangapple
  • 本文由 发表于 2022年3月4日 04:27:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/71343405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定