Help with joinLeft in 3.0.x

97 views
Skip to first unread message

cra...@tataryn.net

unread,
Sep 8, 2016, 10:05:12 PM9/8/16
to Slick / ScalaQuery
I'm converting some code from Slick 2.1 to 3.0.3, and when I migrated my join from using leftJoin to joinLeft I'm receiving this error:

[error] ContentRepoLocal.scala:84: constructor cannot be instantiated to expected type;
[error]  found   : (T1, T2)
[error]  required: slick.lifted.Rep[Option[(repo.model.UserContentTable, repo.model.ContentTable)]]
[error]           .map { case (u, (uc, c)) => (u, c.optionProjection) }
[error]                           ^
[error] ContentRepoLocal.scala:84: diverging implicit expansion for type slick.lifted.Shape[_ <: slick.lifted.FlatShapeLevel, Nothing, T, G]
[error] starting with method repColumnShape in trait RepShapeImplicits
[error]           .map { case (u, (uc, c)) => (u, c.optionProjection) }


On this code:

def getContentsByUser(userId: UUID): Either[UserNotFoundError, List[Content]] = {

 val subQuery
=
   
for {
     uc
<- UserContentTable.query if uc.userId === userId.toString && (!uc.adopted.isDefined || !uc.adopted)
     c
<- ContentTable.query if uc.contentId === c.id
   
} yield (uc, c)


 val query
=
   
UserTable.query.filter(_.id === userId.toString)
     
.joinLeft(subQuery).on { case (u, (uc, c)) => u.id === uc.userId}
     
.map { case (u, (uc, c)) => (u, c.optionProjection) }

 
//...

}


Has anyone come across a similar error and know how to fix it?

Thanks,

Craig.






cra...@tataryn.net

unread,
Sep 9, 2016, 3:40:02 PM9/9/16
to Slick / ScalaQuery
I have created a sample project to help demonstrate the problem:


Any help would be greatly appreciated!

Craig.

cra...@tataryn.net

unread,
Sep 9, 2016, 6:56:18 PM9/9/16
to Slick / ScalaQuery
Commented out the map statement and added the following print statement to see what the types were that were returned from the on function:

    val query =
     
UserTable.query.filter(_.id === userId.toString)
       
.joinLeft(subQuery).on { case (u, (uc, c)) => u.id === uc.userId}

       
//.map { case (u, (uc, c)) => (u, c.optionProjection) }
    println
("The type of the query is:")
    println
(getType(query))


The output was:

The type of the query is:
slick.lifted.Query[(slick3.problem.repo.model.UserTable, slick.lifted.Rep[scala.Option[(slick3.problem.repo.model.UserContentTable, slick3.problem.repo.model.ContentTable)]]),(slick3.problem.repo.model.UserTable#TableElementType, scala.Option[(slick3.problem.repo.model.UserContentModel, slick3.problem.model.Content)]),scala.Seq]

To me that seems to match the types that are required in the error message when I add back in the map :

slick.lifted.Rep[Option[(slick3.problem.repo.model.UserContentTable, slick3.problem.repo.model.ContentTable)]]

Confused...

Craig.

cra...@tataryn.net

unread,
Sep 10, 2016, 12:03:17 PM9/10/16
to Slick / ScalaQuery
Got a little bit further with this by refactoring my subQuery to use the for comprehension syntax:

    val query =
     
for (
     
(u, t) <- UserTable.query.filter(_.id === userId.toString) joinLeft subQuery on { case (u, (uc, c)) => u.id === uc.userId }
     
) yield (u, t)

    println
("The type of the query is:")
    println
(getType(query))

   
()
 
}




^^^ This compiles. However, according to the documentation [1] the yield should be applying t.map(_) to convert "Null" values to None.

So when I refactor the line to read:

yield (u, t.map(_))

I get the error:

[error] missing parameter type for expanded function ((x$2) => t.map(x$2))
[error]       ) yield (u, t.map(_))
[error]                         ^
[error] one error found

Craig

cra...@tataryn.net

unread,
Sep 12, 2016, 10:33:30 PM9/12/16
to Slick / ScalaQuery
A fellow responded on the issue tracker with the following:

joinLeft joins subQuery as Option. In this case, you have to re-map subQuery as follows:

val query =
   
UserTable.query.filter(_.id === userId.toString)
     
.joinLeft(subQuery).on { case (u, (uc, c)) => u.id === uc.userId}

     
.map { case (u, sub) => (u, sub.map { case (uc, c) => c }) }

No more errors.

Thanks,

Craig
Reply all
Reply to author
Forward
0 new messages