我已经为以下代码正确创建了关系:
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/