给出以下表格:
case class Person(id: Int, name: String)
case class Dead(personId: Int)
并填充:
Person(1, "George")
Person(2, "Barack")
Dead(1)
是否可以使用单个查询生成(Person, Option[Dead])
列表,如此?
(Person(1, "George"), Some(Dead(1)))
(Person(2, "Barack"), None)
答案 0 :(得分:1)
对于光滑的3.0,它应该是这样的:
val query = for {
(p, d) <- persons joinLeft deads on (_.id === _.personId)
} yield (p, d)
val results: Future[Seq[(Person, Option[Dead])]] = db.run(query.result)
在光滑的情况下,外部联接会自动包裹在Option
类型中。您可以阅读有关加入此处的更多信息:http://slick.typesafe.com/doc/3.0.0/queries.html#joining-and-zipping