问题
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