smaiakov
smaiakov

Reputation: 480

How to write nested queries in select clause with slick 3.2 +

Is there any way to create nested select using slick 3.2+ ? Basically all that I need described here How to write nested queries in select clause

However on slick 3.2 this approach does not work.

Upvotes: 3

Views: 1612

Answers (1)

Mateusz Kubuszok
Mateusz Kubuszok

Reputation: 27595

If you have tables Users (id: UUID, email: String) and Persons (userId: UUID, name: String, surname: String) than query

select email
from Users
where id in (select userId
             from Persons
             where name = 'John'
             and surname = 'Smith')

would look kind of like:

users
  .filter(
    _.id in persons
              .filter(p => p.name === "John" && p.surname === "Smith")
              .map(_.userId)
  )
  .map(_.email)
  .result

Things you need to remember:

  • Query type is not DBIO (nor DBIOAction) - if you want to compose queries, you need to do it before calling .result on them
  • when you are using subquery in condition use in instead of inSet

Same principle should hold whether you use in, join, etc.

Upvotes: 1

Related Questions