我在User
和TrainerProp
(用户属性)之间有N:M关联。这是简化的示例:
/**
* User
*/
const User = sequelize.define('User', {
// USER ATTRIBUTES... NOT IMPORTANT
});
User.associate = models => {
// User have many TrainerProps
User.belongsToMany(TrainerProp, {
through: 'user_trainerProp',
foreignKey: 'user_id'
});
};
/**
* TrainerProp
*/
const TrainerProp = sequelize.define('TrainerProp', {
// TRAINER PROP ATTRIBUTES... NOT IMPORTANT
});
TrainerProp.associate = models => {
// TrainerProp belongs to many Users
TrainerProp.belongsToMany(User, {
through: 'user_trainerProp',
foreignKey: 'trainerProp_id',
onDelete: 'restrict',
onUpdate: 'restrict'
});
};
/**
* user_trainerProp table (n:m)
*/
const user_trainerProp = sequelize.define('user_trainerProp', {
trainerProp_id: DataTypes.INTEGER,
user_id: DataTypes.INTEGER,
}, {
timestamps: false,
freezeTableName: true
});
如您所见,我对Trainer
进行了onUpdate和onDelete限制,因此当我尝试删除TrainerProp
表user_trainerProp
中使用的内容时,它将抛出ForeignKeyConstraintError
我的问题是,当我尝试删除User
有TrainerProps
的内容时,我也受到限制。我希望,当我删除User
时,user_trainerProp
中的所有行也将被删除,并且不会出现错误...
如何实现?