使用源模型续订连接表和过滤器

时间:2018-03-30 22:38:33

标签: postgresql sequelize.js

我已正确创建关系,以便以下代码正常工作:

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

这包括与频道相关的所有交易。

但是,我希望对此进行过滤,以仅包含保存在Channel实例本身中的nonce最高的事务。

例如,我认为它应该如何工作,但这绝对不起作用:

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 }]
  }
})

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

1 个答案:

答案 0 :(得分:1)

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

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

这只是原始查询,因为我不知道表格的所有字段,但这是你如何得到预期结果。