ON DELETE CASCADE for multiple foreign keys with Sequelize
I had to set the allowNull:false
for the foreignKey
for getting 'CASCADE' on deletions to work. So it should be something like this in your case:
TaskListEntry.belongsTo(TaskList, {
onDelete: 'cascade',
foreignKey: { allowNull: false } // <-------------
hooks: true
});
Given the case, that your models are in general similar to this structure from http://docs.sequelizejs.com/manual/associations.html#belongs-to-many-associations:
class User extends Model {}
User.init({}, { sequelize, modelName: 'user' })
class Project extends Model {}
Project.init({}, { sequelize, modelName: 'project' })
class UserProjects extends Model {}
UserProjects.init({
status: DataTypes.STRING
}, { sequelize, modelName: 'userProjects' })
User.belongsToMany(Project, { through: UserProjects })
Project.belongsToMany(User, { through: UserProjects })
can you try
TaskListEntry.belongsTo(TaskList);
TaskListEntry.belongsTo(Task);
instead of
TaskListEntry.belongsToMany(TaskList);
TaskListEntry.belongsToMany(Task);
Because, from my understanding of this problem, a single TaskListEntry record can only belong to a single Task and a single TaskList.
Or Are you trying to establish a Many-to-Many relationship here? In that case, I don't think this is the ideal way of implementation.