如何过滤由左连接生成的可选表 [英] How to filter on an optional table produced by a left join in slick

查看:65
本文介绍了如何过滤由左连接生成的可选表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对由 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆