sails.js +水线一对多模型关联,删除“多"时会发生什么? [英] sails.js + waterline One-To-Many model association, what should happen when deleting the Many?

查看:91
本文介绍了sails.js +水线一对多模型关联,删除“多"时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在老师(一个)和孩子(很多)之间有一对多的关系.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆