【问题标题】:Find all instances of "modelA" with no asscociated instances of "modelB" using sequelize使用 sequelize 查找没有关联的“model”实例的所有“model”实例
【发布时间】:2016-01-27 22:04:05
【问题描述】:

我正在使用 sequelize,node.js 模块。

我想findAll ModelA 的实例,这些实例没有 ModelB 的关联实例。

ModelA的每个实例hasManyModelB的实例。

这是我目前的尝试,但没有奏效。

ModelA.findAll({
  logging: console.log,
  where: {
    //- Where number of ModelB === 0?
  }
  include: [
    {
      model: ModelB,
      where: {
        //- Where doesn't exist?
      },
    },
  ],
})

我包含了 logging:console.log,以表明它需要输出一个我可以在其他地方运行的 mysql 查询,这就是为什么我不只是在下一个过滤用户在返回所有用户后在一个承诺链中承诺。

【问题讨论】:

  • 你试过where : { yourForeignKeyName : null }吗?

标签: javascript mysql node.js sequelize.js


【解决方案1】:

这应该可以工作

ModelA.findAll({
  logging: console.log,
  include: [
    {
      model: ModelB,
      where: {
        ModelA_id: {$ne: null}
      },
    },
  ],
})

【讨论】:

    猜你喜欢
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 2022-06-25
    • 2014-10-30
    • 2019-09-10
    • 2020-02-22
    • 2017-06-10
    • 2020-10-11
    相关资源
    最近更新 更多