Slick 3.0 many-to-many query with the join as an i

2019-03-27 16:39发布

I've created a many-to-many collection using Slick 3.0, but I'm struggling to retrieve data in the way I want.

There is a many-to-many relationship between Events and Interests. Here are my tables:

case class EventDao(title: String,
                    id: Option[Int] = None)


class EventsTable(tag: Tag)
  extends Table[EventDao](tag, "events") {

  def id = column[Int]("event_id", O.PrimaryKey, O.AutoInc)
  def title = column[String]("title")

  def * = (
    title,
    id.?) <> (EventDao.tupled, EventDao.unapply)

  def interests = EventInterestQueries.query.filter(_.eventId === id)
    .flatMap(_.interestFk)
}


object EventQueries {

  lazy val query = TableQuery[EventsTable]

  val findById = Compiled { k: Rep[Int] =>
    query.filter(_.id === k)
  }
}

Here's EventsInterests:

case class EventInterestDao(event: Int, interest: Int)


class EventsInterestsTable(tag: Tag)
  extends Table[EventInterestDao](tag, "events_interests") {

  def eventId = column[Int]("event_id")
  def interestId = column[Int]("interest_id")

  def * = (
    eventId,
    interestId) <> (EventInterestDao.tupled, EventInterestDao.unapply)

  def eventFk = foreignKey("event_fk", eventId, EventQueries.query)(e => e.id)
  def interestFk = foreignKey("interest_fk", interestId, InterestQueries.query)(i => i.id)
}


object EventInterestQueries {
  lazy val query = TableQuery[EventsInterestsTable]
}

And finally Interests:

case class InterestDao(name: String,
                       id: Option[Int] = None)

class InterestsTable(tag: Tag)
  extends Table[InterestDao](tag, "interests") {

  def id = column[Int]("interest_id", O.PrimaryKey, O.AutoInc)
  def name = column[String]("name")
  def name_idx = index("idx_name", name, unique = true)

  def * = (
    name,
    id.?) <> (InterestDao.tupled, InterestDao.unapply)

  def events = EventInterestQueries.query.filter(_.interestId === id)
    .flatMap(_.eventFk)
}


object InterestQueries {

  lazy val query = TableQuery[InterestsTable]

  val findById = Compiled { k: Rep[Int] =>
    query.filter(_.id === k)
  }
}

I can query and retrieve tuples of (event.name, interest) with the following:

val eventInterestQuery = for {
  event <- EventQueries.query
  interest <- event.interests
} yield (event.title, interest.name)

Await.result(db.run(eventInterestQuery.result).map(println), Duration.Inf)

So this is what I currently have.

What I want is to be able to populate a case class like:

case class EventDao(title: String,
                interests: Seq[InterestDao],
                id: Option[Int] = None)

The trouble is that if I update my case class like this, it messes up my def * projection in EventsTable. Also, I'll have to rename the EventsTable.interests filter to something like EventsTable.interestIds which is a bit ugly but I could live with if necessary.

Also, I can't find a way of writing a for query that yields (event.name, Seq(interest.name)). Anyway, that's just a stepping stone to me being able to yield a (EventDao, Seq(InterestDao)) tuple which is what I really want to return.

Does anyone know how I can achieve these things? I also want to be able to 'take' a certain number of Interests, so for some queries all would be returned, but for others only the first 3 would be.

2条回答
啃猪蹄的小仙女
2楼-- · 2019-03-27 16:59

So after reading this page and chatting on the mailing list, I finally got it working:

val eventInterestQuery = for {
  event <- EventQueries.query
  interest <- event.interests
} yield (event, interest)

Await.result(db.run(eventInterestQuery.result
  // convert the interests to a sequence.
  .map {
  _.groupBy(_._1)
    .map {
      case (k,v) => (k, v.map(_._2))
  }.toSeq
}
), Duration.Inf)
查看更多
仙女界的扛把子
3楼-- · 2019-03-27 17:24

The only issue with groupBy is you lose order. You could fold the result. I've written this helper for my current project:

def foldOneToMany[A, B](in: Seq[(A, Option[B])], eq: (A, B) => Boolean)
                        (f: (A, B) => A): Seq[A] = 
  in.foldLeft(List.empty[A]) {
    case (head :: tail, (_, Some(rel))) if eq(head, rel) =>
      f(head, rel) :: tail
    case (r, (el, Some(rel))) => f(el, rel) :: r
    case (r, (el, None)) => el :: r
  }.reverse

It could do with some love. Now it takes in a function A,B => Boolean to determine if B belongs to A and a function A,B => A that adds B to A.

Virtualeyes also has a point. In Postgres you could use array_agg function to use a little less bandwidth from the db.

查看更多
登录 后发表回答