Scala Slick pagination compiles, but crash on running

I have slick join query in a one to many relation

val query = for {
((u,a),j) <- users join address on(_.idUser === _.idUser) join
               jobs on(_._1.idUser == _.idUser)
} yield (u,a,j)

val res = db.run(query.groupBy(_._1.idUser).flatMap(_._2)
                  .drop((page - 1) * perPage).take(perPage).result)

I need to have unique users, that's why I am groupping by idUser first. The thing is the code compiles, but when I run I have a slick error: "slick.SlickTreeExceptions: Unresolved monadic join: Non-Pure select clause in Bind s2"

Any help on how to achieve this pagination would be most appreciated. Thanks

Upvotes: 1

Views: 329

Answers (1)

Dmytro Mitin
Dmytro Mitin

Reputation: 51723

_._1.idUser == _.idUser should be _._1.idUser === _.idUser

SlickTreeException is thrown when monadic joins are desugared to applicative joins.

Monadic joins are created with flatMap. They are theoretically more powerful than applicative joins because the right-hand side may depend on the left-hand side. However, this is not possible in standard SQL, so Slick has to compile them down to applicative joins, which is possible in many useful cases but not in all of them (and there are cases where it is possible in theory but Slick cannot perform the required transformation yet). If a monadic join cannot be properly translated, it will fail at runtime.

Can you change .groupBy(_._1.idUser).flatMap(_._2) with .sortBy(_._1.idUser)?

Upvotes: 1

Related Questions