scala - 光滑的左外连接获取整个连接行作为选项

标签 scala left-join option slick

我的加入看起来像这样:

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

但是当 user.imageId 为 null 时,slick 在运行时失败

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



将产量更改为
} yield (user, image.?)

给了我一个编译时异常,它只适用于单个列

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



会有不同的方式来完成我在这里尝试做的事情吗? (在单个查询中)

最佳答案

使用下面的代码,你可以把它写成:产量(用户,图像。也许)

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
    } 
}

关于scala - 光滑的左外连接获取整个连接行作为选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14990365/

相关文章:

scala - 如何在 CircleCI 上添加 SBT 插件解析器?

php - 如何INNER JOIN 两个以上的类别?

php - 从mysql拉取时如何在php选项值中添加计算功能?

php - 从PHP/MySQL INSERT INTO表生成选择选项

linux - 如何使用 Scala 实现 ls -l

scala - 如何一次到 "find"序列元素和谓词结果?

scala - Scala : cannot declare reference to function using supertypes

php - 如何在此查询中使用 LEFT JOIN

sql - 根据给定的开放日期和关闭日期,生成一段时间内开放门票的计数

php - php:onchange选项选择更新总计