问题描述
大家好。我有两个表(项目,配置)作为belongsToMany(ProjectConfigurations)连接。我需要删除项目(Project),因此,我需要删除与该项目相关的配置。我该怎么做?
解决方法
您需要在 '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;
};
...
并在 'configurations.model.js'
中:
const Configurations = sequelize.define('configurations',other_stuffs);
...
Configurations.associate = function (models) {
this.belongsToMany(models.get('projects'),foreignKey: 'configurations_id',});
return this;
};
...