【问题标题】:How to filter table with entity from other tables related by many to many relationship using GORM?如何使用 GORM 从多对多关系相关的其他表中过滤具有实体的表?
【发布时间】:2019-10-27 13:55:13
【问题描述】:

我有 Product 表,它使用多对多关系与其他两个表 Categorie & AttributeValue 连接。我正在使用 GORM 作为 ORM。这些表的 go struct 如下所示。

type Product struct {
    ProductID               int                  `gorm:"column:product_id;primary_key" json:"product_id"`
    Name                    string               `gorm:"column:name" json:"name"`
    Categories              []Category           `gorm:"many2many:product_category;foreignkey:product_id;association_foreignkey:category_id;association_jointable_foreignkey:category_id;jointable_foreignkey:product_id;"`
    AttributeValues         []AttributeValue     `gorm:"many2many:product_attribute;foreignkey:product_id;association_foreignkey:attribute_value_id;association_jointable_foreignkey:attribute_value_id;jointable_foreignkey:product_id;"`
}


type Category struct {
    CategoryID   int         `gorm:"column:category_id;primary_key" json:"category_id"`
    Name         string      `gorm:"column:name" json:"name"`
    Products     []Product   `gorm:"many2many:product_category;foreignkey:category_id;association_foreignkey:product_id;association_jointable_foreignkey:product_id;jointable_foreignkey:category_id;"`
}


type AttributeValue struct {
    AttributeValueID int    `gorm:"column:attribute_value_id;primary_key" json:"attribute_value_id"`
    AttributeID      int    `gorm:"column:attribute_id" json:"attribute_id"`
    Value            string `gorm:"column:value" json:"value"`
    Products     []Product   `gorm:"many2many:product_attribute;foreignkey:attribute_value_id;association_foreignkey:product_id;association_jointable_foreignkey:product_id;jointable_foreignkey:attribute_value_id;"`
}

如果我想按类别查询 Product 表,我可以像下面这样操作,它将返回 category_id 为 3 的类别中的所有产品。

cat := model.Category{}
s.db.First(&cat, "category_id = ?", 3)
products := []*model.Product{}
s.db.Model(&cat).Related(&products, "Products")

如果我想同时按 Category 和 AttributeValue 查询 Product 表,我该怎么做?假设我想查找 category_id 为 3 且 AttributeValue 为 attribute_value_id 2 的所有产品?

【问题讨论】:

    标签: sql go go-gorm


    【解决方案1】:

    我找到了一些基于类别和属性值查询产品的方法。我得到的最好方法如下所示

        products := []*model.Product{}
    
        s.db.Joins("INNER JOIN product_attribute ON product_attribute.product_id = " +
                        "product.product_id AND product_attribute.attribute_value_id in (?)", 2).
        Joins("INNER JOIN product_category ON product_category.product_id = " +
                        "product.product_id AND product_category.category_id in (?)", 3).
        Find(&products)
    

    执行此操作后,products slice 将填充 category_id 为 3 且 AttributeValue 为 attribute_value_id 2 的所有产品。如果我们需要在多个 Category 和 AttributeValue 中查找产品,我们可以传递字符串切片。

    【讨论】:

    • 谢谢!如果需要在产品上填充类别切片,Joins(...).Preload("Categories") 似乎可以解决问题。
    猜你喜欢
    • 1970-01-01
    • 2018-06-07
    • 2018-06-22
    • 1970-01-01
    • 2018-01-13
    • 2018-09-15
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多