Sequelize 包含模型的 where 子句被忽略

问题描述 投票:0回答:1

我遇到一个问题,我将 where 子句传递到包含的关联模型中,但 where 子句被忽略。下面是我所拥有的较小样本,因为我无法发布实际的列和数据,但生成的 sql 看起来除了 TableC 的 where 子句之外的所有内容都丢失了。很确定这些关联工作正常,因为我还有其他查询和其他代码,它们可以完美地协同工作,这只是一个条件不起作用...表 C 与表 A 有一个 FK(例如,表 c 有一个列 tableAId ,其中一个fk 与该表的 id)

await TableA.findAll({
  where: {
    id: 1
  },
  include: [
    {
      model: TableB 
    },
    {
      model: TableC,
      where: {
        foo: 'bar'
      }
    }
  ],
});
javascript sql database sequelize.js
1个回答
0
投票
   await TableA.findAll({
  where: {
    id: 1
  },
  include: [
    {
      model: TableB 
    },
    {
      model: TableC,
      where: {
        foo: 'bar'
      },
      required: true
    }
  ],
});

在关联中使用条件时添加所需标志。

© www.soinside.com 2019 - 2024. All rights reserved.