Objection.js如何在内部查询中使用外部值?

问题描述 投票:0回答:1

我正在为一个比较复杂的SQL查询而苦恼,这个查询必须在Objection.js中进行,下面是目前的代码

const tagEntry = await Tag_overview.query()
    .where('playable') //the playable column is a boolean
    .whereExists(
        InnerTableName.query().findById([<normal variable>, tag.id]) //<= tag.id is the id of the row in the outer query
            )
    )
    .orderBy(raw('random()'))// this randomly orders the selection
    .limit(1)

"tag.id "应该是当前正在检查的topouter查询中的行的值。在SQL中,我会用简单的一行来解决,比如(<normal variable>是一个Javascript变量传入查询中,可以作为一个硬编码的值,它和tagid是一个复合键)

and EXISTS (SELECT tagid, othercolumn FROM kunstmakler_preselection WHERE tag.id = tagid AND <normal variable>  = othercolumn)

但我完全不知道如何在Objection.js中做到这一点。是的,它需要一个内部 Query,但我如何将这个 tag.id 传入其中?我完全迷茫了,API Reference和Recipe Book都没有任何帮助(可以在这里找到。 https:/vincit.github.ioobjection.jsrecipes)。 )

这里有必要加盟吗?值得吗?[tagoverview表比较小,而 "InnerTableName "比较大]。我觉得这不可能是真正的解决方案,因为在ülain SQL中,这将是一个如此顺畅的单行道。

javascript sql postgresql objection.js
1个回答
1
投票

首先确保你在model中正确声明了复合键。InnerTableName https:/vincit.github.ioobjection.jsipescomposite-keys.html。

那么你应该可以做到。

.whereExists(
  InnerTableName.query().findById([theJsVariable, ref("Tag_overview.id")])
)
© www.soinside.com 2019 - 2024. All rights reserved.