use*_*106 6 orm node.js sails.js waterline
我在教师(一)和儿童(很多)之间有一对多的关系.
如果我做:
Teacher.destroy(teacherId).exec(function(err){});
Run Code Online (Sandbox Code Playgroud)
孩子们不会被自动删除.
这是一个错误还是我应该手动删除它们?如果这不是一个错误,那么不删除孩子的解释是什么?
sgr*_*454 17
Waterline目前不支持级联删除.它可能是未来版本中的配置选项,但它可能永远不会是默认配置.在大多数生产就绪的应用程序中,您可能应该进行软删除.在任何一种情况下,您都可以通过使用afterDestroy生命周期回调来获得所需的内容.
在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);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用该afterUpdate方法执行类似于软删除的操作.
| 归档时间: |
|
| 查看次数: |
2549 次 |
| 最近记录: |