【问题标题】:How can I concat a relation in a Sequelize 'where'?如何在 Sequelize 'where' 中连接关系?
【发布时间】:2020-01-07 21:25:06
【问题描述】:

如何在 where 查询中组合 关系 中的两个字符串列? 我有一个表 Car 与表 Owner 有关系。

我尝试了以下方法,但失败并出现错误

SequelizeDatabaseError:缺少表“carOwner”的 FROM 子句条目

  const result = Car.findAll({
    where: sequelize.where(
      sequelize.fn(
        'concat',
        sequelize.col('carOwner.firstName'),
        ' ',
        sequelize.col('carOwner.lastName'),
      ),
      {
        [op.iLike]: `%${query}%`,
      },
    ),
  })

【问题讨论】:

标签: database postgresql sequelize.js


【解决方案1】:

我认为您错过了包含关联模型 CarOwner

Car.findAll({
    where: sequelize.where(
        sequelize.fn(
            'concat',
            sequelize.col('carOwner.firstName'),
            ' ',
            sequelize.col('carOwner.lastName'),
        ), {
            [op.iLike]: `%${query}%`,
        },
    ),
    include : {
        model : CarOwner , // <---- HERE
        required : true
    }
})

【讨论】:

  • 如何在jsonb中连接属性而不是列?
猜你喜欢
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
相关资源
最近更新 更多