【问题标题】:joinLeft error: constructor cannot be instantiated to expected typejoinLeft 错误:构造函数无法实例化为预期类型
【发布时间】:2016-09-13 10:12:51
【问题描述】:

我正在将一些代码从 Slick 2.1 转换为 3.0.3,当我将我的加入从使用 leftJoin 迁移到 joinLeft 时,我收到了这个错误,我不知道如何解决它:

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

关于此代码:

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

 //...

}

编辑 1:

通过重构我的 subQuery 以使用 for 理解语法,进一步了解了这一点:

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)

这编译。但是,根据documentationyield 应该应用t.map(_)Null 值转换为None

所以当我重构该行时:

yield (u, t.map(_))

我得到错误:

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

编辑 2:您可以找到一个最小、完整和可验证的示例 here

编辑 3: 确认 Slick 3.1.1 中也存在此问题

【问题讨论】:

  • 您应该发布表格的定义,因为没有它们我们将无法重现您的错误。
  • @PawełJurczenko 我创建了一个项目来隔离和演示问题。它应该有助于填补空白github.com/ctataryn/slick3-problems
  • 您将问题与 Edit1 混淆了,它要求我们解决一个完全不同的错误。如果有人来这里搜索第一条错误消息,他们希望找到那个的答案。
  • @GreenAsJade 我相信这是同样的错误,因为没有推断出元组的类型。
  • 啊 - 这对我来说并不明显:编辑案例中是否还有更多错误消息,您没有分享?

标签: scala slick


【解决方案1】:

joinLeft 在 Slick3 中以 Option 的身份加入 subQuery。在这种情况下,您必须重新映射subQuery,如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 2019-12-16
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多