光滑的左外部联接获取整个联接行作为选项 [英] Slick left outer join fetching whole joined row as option

查看:81
本文介绍了光滑的左外部联接获取整个联接行作为选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的加入看起来像这样:

My join looks like this:

def byIdWithImage = for {
    userId <- Parameters[Long]
    (user, image) <- Users leftJoin RemoteImages on (_.imageId === _.id) if user.id === userId
} yield (user, image)

但是当user.imageId为null时,滑动会在运行时失败

but slick fails at runtime when user.imageId is null

[SlickException:读取列RemoteImage.url的NULL值]

[SlickException: Read NULL value for column RemoteImage.url]

将产量更改为

} yield (user, image.?)

为我提供了一个编译时异常,它仅适用于单个列

gives me a compile time exception, it only works on individual columns

找不到类型为scala.slick.lifted.TypeMapper [image.type]的证据参数的隐含值

could not find implicit value for evidence parameter of type scala.slick.lifted.TypeMapper[image.type]

会有其他方法来完成我在这里要做的事情吗? (在单个查询中)

Would there be a different way to accomplish what I'm trying to do here? (in a single query)

推荐答案

使用以下代码,您可以将其表示为: yield(用户,image.maybe)

With the code below you can put it like: yield (user, image.maybe)

case class RemoteImage(id: Long, url: URL)

class RemoteImages extends Table[RemoteImage]("RemoteImage") {
    def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
    def url = column[URL]("url", O.NotNull)
    def * = id.? ~ url <> (RemoteImage.apply _, RemoteImage.unapply _)

    def maybe = id.? ~ url.? <> (applyMaybe,unapplyBlank)

    val unapplyBlank = (c:Option[RemoteImage])=>None        

    val applyMaybe = (t: (Option[Long],Option[URL])) => t match {
        case (Some(id),Some(url)) => Some(RemoteImage(Some(id),url))
        case _ => None
    } 
}

这篇关于光滑的左外部联接获取整个联接行作为选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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