Sequelize hasMany Join Association

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).

+5
source share
2 answers

The problem is that the relationship should be determined in both directions. Currently, Sequelize knows how to get from Food → Meal due to

 db.food.hasMany(db.meal, {as : 'Food', foreignKey : 'idFood'}); 

but he does not know how to get from food → Food. This is why you should define the same relation on the contrary, for example:

 db.meal.belongsTo(db.food, {foreignKey : 'idFood'}); 

This does not add any new keys, as it will determine meal.idFood , which you already defined using your first statement.

Now you can complete

 db.meal.findAll({ include : [db.food] }).then(function (meals) { console.log(JSON.stringify(meals)); <-- each array element of meals should have an attribute `food` }); 
+6
source

If you use an alias in the association ( { as: 'Food' } ), you also need to use it in the include statement.

So it will be like this:

 db.meal.findAll({ include : [{ model: db.food, as: 'Food' }] }).then(function (meals) { console.log(JSON.stringify(meals)); }); 

If the association is smooth (using the as option), you must specify this alias when you enable the model. Notice how custom tools get crowded out like Tools above. To get this right, you must specify the model you want to download, as well as an alias

More details here .

0
source

All Articles