1
votes

I'm having a really hard time trying to execute a join on two tables.

Here is my model in Scala :

case class Event(idEvent: Int, blablabla)
case class User(idUser: Int, blablabla)
case class UserParticipatesToEvent(idUser: Int, idEvent: Int)

Basically, a user can attend many different events.

I need to fetch all the users attending an event. Earlier, in slick 2.0, a simple for-comprehension in my User DAO was doing the job:

def findUsersByEvent(idEvent: Int): List[User] = {
   (for {
     userIds <- EventDAO.findIDUsersByEvent(idEvent)
     res <- this.findUserById(userIds)
   } yield(res))
}

In slick 3.0, I'm not at all able to figure out how to achieve this simple join. Nowadays, in Slick 3.0, it should return a Future[List[User]].

Thanks in advance for your help.

1

1 Answers

4
votes

Here you have nicely explained how to do simple joins, and join many to many tables(like in your case). You basically have 2 options, inner join(doable with if statements in for comprehensions) or outer join(left or right join).

Inner join:

val usersWithEvents = for {
  ((user, _), event) <- UserTable.join(UserEventTable).on(_.userId === _.userId).
                   join(EventTable).on(_._2.eventId === _.eventId)
} yield(user, event)

Now you have Seq[(User, Event)], so you group by User and you'll get user's events.

Note: I didn't test this...

See the repo of the tutorial also. Hope this helps! :)