sails.js +水线一对多模型关联,删除“多"时会发生什么? [英] sails.js + waterline One-To-Many model association, what should happen when deleting the Many?
问题描述
我在老师(一个)和孩子(很多)之间有一对多的关系.
I have a one to many relation between Teacher(One) and Children(Many).
如果我这样做:
Teacher.destroy(teacherId).exec(function(err){});
子代不会自动删除.
是错误还是应该手动删除? 如果那不是错误,那么不删除子级的解释是什么?
Is it a bug or should I delete them manually? If that's not a bug, what is the explanation for not deleting children?
推荐答案
水线当前不支持级联删除.在将来的版本中,它可能是配置选项,但可能永远不会是默认选项.在大多数可用于生产环境的应用程序中,您可能仍应该进行软删除.无论哪种情况,都可以使用 afterDestroy
生命周期回调来获得所需的内容.
Waterline currently doesn't support cascading deletes. It may be a configuration option in future versions, but it will probably never be the default. In most production-ready apps you probably should be doing soft-deletes anyway. In either case, you can get what you want by using the afterDestroy
lifecycle callback.
在api/models/Teacher.js
中,类似于:
module.exports = {
attributes: {
// attributes here
},
afterDestroy: function(destroyedRecords, cb) {
// Destroy any child whose teacher has an ID of one of the
// deleted teacher models
Child.destroy({teacher: _.pluck(destroyedRecords, 'id')}).exec(cb);
}
}
您可以使用afterUpdate
方法对软删除执行类似的操作.
You could do something similar with soft-deletes using the afterUpdate
method.
这篇关于sails.js +水线一对多模型关联,删除“多"时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!