Sequelize——查询两个属性?

Sequelize – querying with two attributes?

我有一个搜索框,用户可以在其中输入他的查询,例如John Doe.

我还定义了 User 模型,包含列 first_namelast_name。该模型还有一个 full_name 虚拟列集。

我想搜索用户的全名,我试过这个:

const search = 'John Doe';
await DB.User.findAll({
  where: {
    [Sequelize.Op.or]: {
      first_name: { [Op.iLike]: `%${search}%` },
      last_name: { [Op.iLike]: `%${search}%` },
    },
  },
});

只有 John 或只有 Doe 可行,但如果有人写全名 – 不 – 这是可以理解的。

我试过查询 full_name,但它说该列不存在。

我的问题 – 我应该如何执行这样的查询?

根据这个你应该可以使用

const search = 'John Doe';
await DB.User.findAll({
  where: Sequelize.where(Sequelize.fn('concat', Sequelize.col('first_name'), ' ', Sequelize.col('last_name')), {
    [Op.iLike]: `%${search}%`
  })
});