【问题标题】:Cascade error with a mandatory one-to-many relationship具有强制性一对多关系的级联错误
【发布时间】:2018-12-09 00:37:14
【问题描述】:

我使用的是 Sails 1.1.0。我使用sails-disksails-mongo 适配器尝试了以下操作。

我创建了一个强制性的一对多关系。 Sofa 具有 ownerUser 一对多,Sofa 也具有 homeHome 一对多。但是,当我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


【解决方案1】:

我最近遇到了同样的问题,使用带有inMemoryOnly: true 选项的sails-disk 适配器,您不需要清理数据库,因为它只是内存,测试完成后会自动清理。

datastores: {
  default: {
    adapter: 'sails-disk',
    inMemoryOnly: true
  },
}

【讨论】:

  • 我认为他们在最新版本的sails-disk中修复了这个问题,但我不确定他们是否发布了修复。
猜你喜欢
  • 2014-05-11
  • 2020-03-29
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-03
  • 2011-03-13
相关资源
最近更新 更多