Answer the question
In order to leave comments, you need to log in
What are associations for in Sequelize?
Hello!
Can someone please explain what associations are needed in Sequelize like hasMany, belongsTo etc.? The documentation says that to create a foreign key in the model, but why do I need them if I set the foreign key when creating the table in the migration files, for example:
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Orders', {
name: {
type: Sequelize.STRING(50)
},
category: {
type: Sequelize.INTEGER(5),
references: {
model: "Categories",
key: "id"
},
onUpdate: "CASCADE",
onDelete: "RESTRICT"
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Orders');
}
};
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question