- I am using commands of sequelize to create and migrate Models to generate Tables in MySql database.
- To generate new model:
sequelize model:create --name Demo --attributes column:string - And After the Model and migration file generate, adding few lines of code to make association and foreign key constraints using given snippet of code:
- In Derived/Child Table of Comapny:
Employee.associate = function (models) {
// associations can be defined here
Employee.belongsTo(models.Company, {
foreignKey: "companyId",
onDelete: "CASCADE"
})
};
In Base/Parent table Comapany:
Company.associate = function (models) { // associations can be defined here Company.hasMany(models.Employee,{ foreignKey:"companyId", }) };
But it does not reflect in Table->ALTER TABLE->ForeignKey Constraints.