如何过滤由左连接生成的可选表 [英] How to filter on an optional table produced by a left join in slick
问题描述
我需要对由 Scala slick
中的左连接生成的可选表的属性应用过滤器.我在网上找不到关于此的任何文档或任何类似问题.
I need to apply a filter on an attribute of an optional table produced by a left join in scala slick
. I could not find any documentation on this or any similar questions online.
考虑以下查询:
val query = FirstTable joinLeft SecondTable on (_.foreignId === _.id)
我想过滤的是 SecondTable
的一个属性:
I would like to filter is by an attribute of the SecondTable
:
query.filter {
case (firstTable, secondTableOpt) => secondTableOpt.attribute === "value"
}
显然这不会编译,因为 secondTableOpt
是一个 Rep[Option[SecondTable]]
.Rep
对象上似乎没有 .get
方法.
Obviously this does not compile since secondTableOpt
is a Rep[Option[SecondTable]]
. There does not seem to be a .get
method on the Rep
object.
应该有一种用slick
来写这个的方法,有谁知道如何实现?
There should be a way to write this in slick
, does anyone know how to achieve this?
谢谢
推荐答案
由于需要在结果中对SecondTable
中的结果进行过滤,所以最好在left join之前进行.所以代码将是这样的:
As you need to filter the results in your SecondTable
in the result, it is better if you do it before the left join. So the code will be something like this:
val filteredSecondTable = SecondTable.filter(_.attribute === "value")
val query = FirstTable joinLeft filteredSecondTable on (_.foreignId === _.id)
这篇关于如何过滤由左连接生成的可选表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!