如何在元组上重用Slick Relationships?

时间:2015-11-05 12:31:47

标签: scala playframework slick table-relationships

在Slick 3.1.0中使用documentation作为类的来源:

   implicit class PersonExtensions[C[_]](q: Query[People, Person, C]) {
      def withAddress = q.join(addresses).on(_.addressId === _.id)
      def withContact = q.join(contacts).on(_.contactId === _.id)
   }

我希望能够做到这样的事情:

   val chrisQuery = people.filter(_.id === 2)

   val chrisWithAddressWithContact: Future[((Person, Address), Contact)] =
     db.run(chrisQuery.withAddress.withContact.result.head)
   val chrisWithContactWithAddress: Future[((Person, Contact), Address)] =
     db.run(chrisQuery.withContact.withAddress.result.head)

1 个答案:

答案 0 :(得分:0)

您可以使用zip撰写查询。

val q1: Query[Xs, X, Seq]
val q2: Query[Ys, Y, Seq]
val query: Query[(Xs, Ys), (X, Y), Seq] = q1 zip q2
val results: DBIO[Seq[(X, Y)]] = query.result
val result: DBIO[(X, Y)] = results.head
db.run(result).map { case (r1: X, r2: Y) => ...}

当然,您可以省略类型并内联所有内容:)

Doc:http://slick.typesafe.com/doc/3.0.0/queries.html#zip-joins