I am doing a one-to-many relationship in my codes right now. Below is my db:
TABLE: categories
FIELDS: category_id (primary)
categoryName
TABLE: products
FIELDS: id (primary)
productName
category_id
I want products.category_id communicate with categories.category_id so that it will select products with its corresponding category_id. But what actually happens in my code is it select category_id via the id in my products which is my primary key and it goes like this:
SELECT `Category`.`category_id`, `Category`.`categoryName`, `Category`.`description`, `Category`.`picture` FROM `categories` AS `Category` WHERE `Category`.`category_id` IN (3, 5, 6)
the values: 3,5,6 are the primary keys of the three products in my products table. But it should be this way
SELECT `Category`.`category_id`, `Category`.`categoryName`, `Category`.`description`, `Category`.`picture` FROM `categories` AS `Category` WHERE `Category`.`category_id` IN (1, 2, 3)
where the values: 1,2,3 are the category_id in my products table corresponding with the category_id in my categories table
Any help would be highly appreciated.
Make sure your model associations are set up correctly:
And change
Category.category_id to justCategory.id.