postgresql - Sequelize 连接表并使用源模型过滤

标签 postgresql sequelize.js

我已经为以下代码正确创建了关系:

const channels = await Channel.findAll({
  include: [
    {
      model: Transaction
    }
  ],
  where: {
    [Op.or]: [{ agentA: address }, { agentB: address }]
  }
})

这包括与 channel 关联的所有事务。

但是,我想过滤它以仅包含具有最高 nonce 的事务,该事务保存在 Channel 实例本身中。

例如,这就是我认为它应该工作的方式,但这绝对行不通:
const channels = await Channel.findAll({
  include: [
    {
      model: Transaction,
      where: {
        nonce: this.latestNonce // i want the latestNonce from the channel we are currently joining on
      }
    }
  ],
  where: {
    [Op.or]: [{ agentA: address }, { agentB: address }]
  }
})

有谁知道如何使这项工作?

最佳答案

对于最新的交易条目,您可以这样做,

{
    model: Transaction ,
    required : false ,
    limit : 1 ,
    order : [['id' ,'desc']]
}

这只是原始查询,因为我不知道您表的所有字段,但这是您获得预期结果的方式。

关于postgresql - Sequelize 连接表并使用源模型过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49582318/

相关文章:

PostgreSQL "tuple already updated by self"

sql - 如何在 bash 脚本中对每个命令进行计时(运行一系列 psql 语句)

mysql - 联结表中的复合主键 - Sequelize

node.js - NodeJS 执行 then() 之后的下一个进程

javascript - 如何在 Sequelize.js 中撤消软删除

node.js - 自定义查询的不一致的 Sequelize 查询构造

node.js - 如何在 Sequelize 中用 "in"制作 "subquery"?

sql - 在简单的 SQL 查询中级联选择

javascript - Sequelize findOne/findAll 查询不返回关联属性

postgresql - 如何在 postgres 12 中对分区表的复合主键的列部分添加或模拟唯一性约束?