sails.js + waterline One-To-Many model association, what should happen when deleting the Many?

放肆的年华 提交于 2019-12-01 04:28:33

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.

In api/models/Teacher.js, something like:

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);
    }
}

You could do something similar with soft-deletes using the afterUpdate method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!