我在教师(一)和儿童(很多)之间有一对多的关系。
如果我这样做:
Teacher.destroy(teacherId).exec(function(err){});
不会自动删除孩子。
是个bug还是我应该手动删除? 如果这不是一个错误,那么不删除孩子的解释是什么?
答案 0 :(得分:17)
Waterline目前不支持级联删除。它可能是未来版本中的配置选项,但它可能永远不会是默认配置。在大多数生产就绪的应用程序中,您可能应该进行软删除。在任何一种情况下,您都可以使用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
方法对软删除执行类似的操作。