Skip to content
Advertisement

How to filter table with entity from other tables related by many to many relationship using GORM?

I have Product table which is connected with the other two tables Categorie & AttributeValue using many to many relationships. I am using GORM as an ORM. go struct for those tables are like bellow.

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;"`
}

If I want to query Product table by category I can do it like bellow which will return all products in a category with category_id 3.

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

If I want to query the Product table by both Category & AttributeValue how can I do that? Suppose I want to find all the products that are in category with category_id 3 and has AttributeValue with attribute_value_id 2?

Advertisement

Answer

I found some ways to query products based on category and AttributeValue. The best way I got is like bellow

    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)

After executing this, products slice will be populated with all the products that are in the category with category_id 3 and has AttributeValue with attribute_value_id 2. We can pass slice of string if we need to find products in multiple Category & AttributeValue.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement