scalaleft-joinslickscala-option

Slick left outer join fetching whole joined row as option


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)

but slick fails at runtime when user.imageId is null

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

Changing the yield to

} yield (user, image.?)

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

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)


Solution

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