0

Good day, everyone. I have two tables (Project, Configuration) that are connected as belongsToMany (ProjectConfigurations). I need to delete the project (Project) and, as a result, I need to delete Configurations that are connected with this project. How can I do it?

enter image description here

MuffinColor
  • 21
  • 1
  • 5

1 Answers1

0

Your need use onDelete: 'CASCADE' in 'projects.model.js':

const Projects = sequelize.define('projects', other_stuffs);
...
Projects.associate = function (models) {
  this.belongsToMany(models.get('configurations'), {
    through: 'project_configurations',
    foreignKey: 'project_id',
    onUpdate: 'CASCADE', // optional
    onDelete: 'CASCADE',
  });
  return this;
};
...

and in 'configurations.model.js':

const Configurations = sequelize.define('configurations', other_stuffs);
...
Configurations.associate = function (models) {
  this.belongsToMany(models.get('projects'), {
    through: 'project_configurations',
    foreignKey: 'configurations_id',
    onUpdate: 'CASCADE', // optional
    onDelete: 'CASCADE',
  });
  return this;
};
...
celibacy0297
  • 26
  • 1
  • 2