January 2019
Intermediate to advanced
460 pages
10h 33m
English
Now that we have the relationship configured with the foreign key, it also needs to be configured inside of our Sequelize model.
Go back to the Post model file and replace the associate function with the following:
Post.associate = function(models) { Post.belongsTo(models.User);};
The associate function gets evaluated inside of our aggregating index.js file, where all model files are imported.
We are using the belongsTo function, which tells Sequelize that every post belongs to exactly one user. Sequelize gives us a new function on the Post model, called getUser, to retrieve the associated user. The naming is done by convention, as you can see. Sequelize does all of this automatically.
Do not forget to add ...
Read now
Unlock full access