使用 Option 进行光滑的左/右/外连接 [英] Slick left/right/outer joins with Option

查看:65
本文介绍了使用 Option 进行光滑的左/右/外连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Slick 示例中,有几个连接示例,其中结果列之一可以为空,就像在执行左连接、右连接或外部连接时一样.例如:

In the Slick examples there are a few examples of joining where one of the resulting columns can be nulls, as it can be the case when doing left, right, or outer joins. For example:

val explicitLeftOuterJoin = for {
  (c, s) <- Coffees leftJoin Suppliers on (_.supID === _.id)
} yield (c.name, s.name.?)

但是如果我想返回整个映射对象怎么办?我的意思是:

But what if I want to return the entire mapped object? What I mean is:

val explicitLeftOuterJoin = for {
  (c, s) <- Coffees leftJoin Suppliers on (_.supID === _.id)
} yield (c, s.?)

这似乎不起作用,因为它抱怨找不到 scala.slick.lifted.TypeMapper[Suppliers] 类型的证据参数的隐式值".基本上我希望它返回一个元组列表 (Coffee, Option[Supplier])

This doesn't seem to work as it complains about "could not find implicit value for evidence parameter of type scala.slick.lifted.TypeMapper[Suppliers]". Basically I'd like it to return a list of tuple of (Coffee, Option[Supplier])

为什么这不起作用,有什么解决方法?特别是,因为这工作正常:

Why doesn't this work and what's the fix for it? Especially, since this works fine:

val q = for {
  c <- Coffees
  s <- Suppliers
} yield (c, s)

(我知道那是内连接)

推荐答案

更新: 这将得到解决,并在 2014 年底即将推出的 Slick 3.0 中简单地工作,不再需要以下内容解决方法

这是目前 Slick 的限制.你必须打电话.?在每一列上单独.但是,您可以在表类中放置一个名为 ? 的函数,该函数在中心位置执行此操作,从而获得 .?在完整的行上.这个 play-slick 示例代码包含一个通用的解决方案,涉及一些生成的代码.我们还有一个 PR 可以自动生成 ?方法排队.

This is a limitation of Slick at the moment. You have to call .? on every column individually. You can however place a function called ? in the table class that does this in a central place and thereby get .? on complete rows. This play-slick example code contains a generic solution involving some generated code. We also have a PR that adds auto-generation of a ? method lined up.

从长远来看,我们将支持 Slick 中的外连接变体,其中 Slick 完全了解所涉及的类型并且您不需要指定 .?任何地方.目前,我们必须接受涉及代码生成的变通方法.

In the long run we will support a variant of outer joins in Slick where Slick is fully aware of the types involved and you do not need to specify .? anywhere. For now we have to live with workarounds involving code generation.

这篇关于使用 Option 进行光滑的左/右/外连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆