How to filter on an optional table produced by a left join in slick

我与影子孤独终老i 提交于 2021-02-20 19:14:13

问题


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.

Consider the following query:

val query = FirstTable joinLeft SecondTable on (_.foreignId === _.id)

I would like to filter is by an attribute of the SecondTable:

query.filter {
  case (firstTable, secondTableOpt) => secondTableOpt.attribute === "value"
}

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.

There should be a way to write this in slick, does anyone know how to achieve this?

Thank you


回答1:


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)


来源:https://stackoverflow.com/questions/36864185/how-to-filter-on-an-optional-table-produced-by-a-left-join-in-slick

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!