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.

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.

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

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