I am expanding my application and I need to join the two models that I previously created using Sequelize, they are as follows:
Food
sequelize.define('meal', { mealId: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, quantity: { type: DataTypes.DECIMAL, allowNull: false }, period: { type: DataTypes.INTEGER, allowNull: false } })
Food
sequelize.define('food', { idFood: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, nameFood: { type: DataTypes.STRING, allowNull: false } })
I added the following ratio:
db.food.hasMany(db.meal, {as : 'Food', foreignKey : 'idFood'});
This row adds the idFood column to the Power menu.
A quick explanation of what is happening, Food is a table with many foods (duh) like Bread, Rice, Beans, etc. Nutrition - this is a table that determines which food the user has selected with his or her details.
Therefore, I realized that Meal had a lot of Food (as I added earlier), but Food did not require any relationship with Meal , since it just stores data and does not change after the first filling. But when I tried to join them,
db.meal.findAll({ include : [db.food] }).then(function (meals) { console.log(JSON.stringify(meals)); });
I got the following error:
Unhandled rejection Error: food is not associated to meal!
Can anyone explain what should I do? I think this is related to relationships, but I could not find in the documentation any good explanation of what I should do.
Thanks!
Edit: reading the documentation ( again ), the example makes sense, but I do not think that the example is applicable to my situation, because in their example the User has a Task, and the Task belongs to the User. But in my case, food does not belong to food, because many dishes can have the same food in different quantities (or for different users).