Sequelize.js 按虚拟字段续集查询

Sequelize.js 按虚拟字段续集查询,sequelize.js,Sequelize.js,我有一个有两个字段的模式。一个记录用户,另一个是基于第一个字段的虚拟字段 sequelize.define("Invoice", { is_processing_by: { type: DataTypes.INTEGER, references: "usertable", referencesKey: "id", allowNull: false }, is_processing: { type: DataTypes.VIRTUAL,

我有一个有两个字段的模式。一个记录用户,另一个是基于第一个字段的虚拟字段

sequelize.define("Invoice", {
  is_processing_by: { 
    type: DataTypes.INTEGER,
    references: "usertable",
    referencesKey: "id",
    allowNull: false
  },
  is_processing: { 
    type: DataTypes.VIRTUAL,
    get: function() {
      return this.get("is_processing_by")
    }
  }
}
我试着去做

Invoice.find({where: {is_processing: 123}})
但这给了我一个错误:

Unhandled rejection AssertionError: expected {
  message: 'column Invoice. is_processing does not exist',
  name: 'SequelizeDatabaseError',
  original: {
  [error: column Invoice.is_processing does not exist]
....

有人知道发生了什么吗?谢谢

对于每一个在这方面苦苦挣扎的人(就像我过去所做的那样),您可以使用原始SQL的一部分来创建一个虚拟列,并使用它对行进行排序。像这样:

const { literal } = require('sequelize')
...
const attributes = Object.keys(Users.rawAttributes)
const users = await Users({
  attributes: [
    ...attributes,
    [literal(`
      coalesce(
        nickname,
        concat("firstName", ' ', "lastName")
      )
    `),
    'name']
  ],
  ...
  order: [[literal('"name"'), 'asc']]
})

希望帮助某人;)

据我所知,虚拟字段只能在从数据库检索数据并用于验证或作为方便方法后使用。您不能在查询中使用它们,因为它们不是数据库的一部分@我明白了。我在想,在2-3个字段的基础上生成一个虚拟字段,如
expiredAftertenutes and UsedByHomeuse
,然后直接查询,而不是使用大量条件进行查询,这样会比较方便。。我想可能没有更简单的方法可以做到这一点?您可以始终在模型上创建一个类方法,例如,您可以执行类似models.someModel.getAllExpiredWithSomeParameter()的操作。但我个人会在服务/业务层而不是模型本身中进行,并从我的UI层调用它。这也有道理。谢谢