【发布时间】:2018-12-09 00:37:14
【问题描述】:
我使用的是 Sails 1.1.0。我使用sails-disk 和sails-mongo 适配器尝试了以下操作。
我创建了一个强制性的一对多关系。 Sofa 具有 owner 与 User 一对多,Sofa 也具有 home 与 Home 一对多。但是,当我await Home.destroy(homeId);Home 时,它不会级联并删除Sofa。相反,我得到错误:
PropagationError: Failed to run the "cascade" polyfill. Could not propagate the potential destruction of this home record.
Details:
Cannot wipe the contents of association (`sofas`) because there is one conflicting shame record whose `home` cannot be set to `null`. (That attribute is required.)
This error originated from the fact that the "cascade" polyfill was enabled for this query.
Tip: Try reordering your .destroy() calls.
[?] See https://sailsjs.com/support for more help.
这是我的模型关联:
// api\models\Sofa.js
owner: {
model: 'user',
required: true
},
home: {
model: 'home',
required: true
},
// api\models\User.js
sofas: {
collection: 'sofa',
via: 'owner'
},
// api\models\Home.js
sofas: {
collection: 'sofa',
via: 'home'
},
有人知道为什么会这样吗?
【问题讨论】:
-
如果你改用
.archive(),你会得到同样的结果吗? -
在您的
config/locals/modals.js中有cascadeOnDestroy: true吗? -
你有没有解决这个问题,查看 casade 的文档,它说“因此删除具有 2 路复数关联(一对多或多对多)的记录将还干净地删除所有指向其他记录的链接(通过删除连接表行或将外键值设置为空)”。我会冒险猜测,在您的情况下,它只是试图将 Sofa 中的外键设置为 null,这会引发您的错误。虽然不理想,但您可以添加一个 beforeDestroy 生命周期回调来为您执行删除过程。我相信这是在帆版 中处理此问题的唯一方法
-
是的,所以对 Sails Trello 进行进一步挖掘,我相信它会像我想的那样工作,级联正在尝试将 Sofa 中的 Home 引用设置为 null。我正在考虑另一种解决方案,您能否从 Sofa 中的 home 引用中删除
required : true,然后触发清理操作,删除 home 引用为 null 的所有 Sofa。 -
哈哈。我靠摩托“如果它没有坏,就把它修好,直到它坏了”。
标签: sails.js