【问题标题】:Sequelize query where on childrenSequelize 查询 where on children
【发布时间】:2019-12-07 14:05:36
【问题描述】:

我有一个用户和一个国家模型。用户属于许多国家,反之亦然。 如何查询countries数组中只有特定国家ID的用户?

const users = await DB.User.findAll({
  where: {
    // What to put here?
  },
  include: [
    {
      model: DB.Country,
      as: 'countries',
      // Putting where here only causes the countries to filter
    },
  ],
});

【问题讨论】:

    标签: postgresql sequelize.js


    【解决方案1】:

    由于您希望任何用户具有特定国家/地区,因此无需在用户的 where 子句中放置任何内容,请将 where 子句放在国家/地区 include 部分。

    const users = await DB.User.findAll({
      where: {
        // Nothing to put here
      },
      include: [
        {
          model: DB.Country,
          as: 'countries',
          where:{
              id : YOUR_EXPECTED_COUNTRY_ID
          }
        },
      ],
    });
    

    【讨论】:

      猜你喜欢
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-09
      • 2012-06-04
      • 2018-07-03
      • 2018-09-25
      • 1970-01-01
      相关资源
      最近更新 更多