Alexandre Curreli
Alexandre Curreli

Reputation: 53

How to filter on an optional table produced by a left join in 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.

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

Upvotes: 5

Views: 1874

Answers (1)

Carlos Vilchez
Carlos Vilchez

Reputation: 2804

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)

Upvotes: 9

Related Questions