Slick data after join needs to be mapped to access the _._1._2._1 etc results, any better way?

81 Views Asked by At

When I do a query using Scala Slick I then need to map the results to access them and flatten them into a (). The problem here is that the joins produce many _._1._2._3._1 etc. Is there a better way to do this?

Events.events.filter( (i: Events) => {
  ((i.isGhost === true) && (i.ghostOfEventId === groupId)) ||
  ((i.isGhost === false) && (i.id === groupId))
})
.join(ProfileEvents.profileEvents).on(_.id === _.eventId)
.join(Profiles.profiles).on(_._2.profileId === _.id)
.join(ProfileEx1s.profileEx1s).on(_._2.id === _.profileId)
.run.toList.map( (n) => {
  (n._1._1._1, // Event
    n._1._1._2, // ProfileEvent
  n._1._2, // Profile
  n._2) // ProfileEx1s
})
1

There are 1 best solutions below

0
On BEST ANSWER

You can do this with a for-comprehension:

val query = for {
  e  <- Events.events if (e.isGhost && e.ghostOfEventId === groupId) || (!e.isGhost && e.id === groupId)
  pe <- ProfileEvents.profileEvents if e.id === pe.eventId
  p  <- Profiles.profiles if pe.profileId === p.id
  px <- ProfileEx1s.profileEx1s if pe.id === px.profileId
} yield (e, pe, p, px)

query.run.toList // <- this will be of type List[(Event, ProfileEvent, Profile, ProfileEx1s)]

I probably mixed up the join conditions, but you should get the idea behind.