【问题标题】:Query model based on value inside one of its columns基于其列之一内的值查询模型
【发布时间】:2023-01-30 16:46:44
【问题描述】:

我有一个名为 staff 的续集模型。员工列之一称为位置,它包含该员工可用的所有位置的 ID。我想创建一个查询,使用位置 ID 让所有员工在该位置处于活动状态。我怎样才能使用 ORM 做到这一点?

【问题讨论】:

    标签: mysql node.js express sequelize.js


    【解决方案1】:

    在 Sequelize 中,您可以执行查询以使用查询中的 include 和 where 选项根据他们的位置过滤员工。

    const staff = await Staff.findAll({
      include: [
        {
          model: Location,
          as: 'locations',
          where: { id: locationId },
          through: { attributes: [] }
        }
      ]
    });
    

    【讨论】:

      猜你喜欢
      • 2021-09-03
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      • 2022-11-22
      • 2021-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多