Query with Sequelize



我正在制作这样的续集查询:

const users = await User.findAll({
    attributes: ['id', Sequelize.literal(`"firstName" || ' ' || "lastName" as name`)],
    where: { status: `[${USER_STATUS.ACTIVE}]`, organizationId: orgId }
  });

我有问题

" andyman_users"。"状态" ='" [1]"'

我希望它做出这样的查询:

" andybom_users"。"状态" ='[1]'

没有双引号。谢谢。我尝试多种方式,但没有起作用。任何帮助都将受到赞赏!

我在stackoverflow上回答:

where: { status: { [Op.contains]: Sequelize.literal(`[${USER_STATUS.ACTIVE}]`) }, organizationId: orgId }

但是查询是

" andybom_users"。"状态" @> [1]

也许这是续集的错误?

您可以事先尝试将${USER_STATUS.ACTIVE}的值存储在变量中,然后将该变量传递为status

const st = "[" + ${USER_STATUS.ACTIVE} + "]";
const users = await User.findAll({
    attributes: ['id', Sequelize.literal(`"firstName" || ' ' || "lastName" as name`)],
    where: { status: st, organizationId: orgId }
  });

我尝试此操作,它有效:

  const users = await User.findAll({
    attributes: [
      'id',
      'firstName',
      'lastName',
      Sequelize.literal(`"firstName" || ' ' || "lastName" as name`)
    ],
    where: { status: { [Op.contains]: [USER_STATUS.ACTIVE] }, organizationId: orgId },
    include: [{ model: Role, as: 'roles', where: { roleName: role } }]
  }).map(u => {
    return u.dataValues;
  });

最新更新