Slick left outer join fetching whole joined row as option

后端 未结 3 1866
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 10:48

My join looks like this:

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


        
3条回答
  •  情歌与酒
    2021-02-03 11:25

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

提交回复
热议问题