【问题标题】:slick filter or where no longer support logical operations?光滑的过滤器或不再支持逻辑操作的地方?
【发布时间】:2014-05-26 20:42:20
【问题描述】:

我使用 slick 2.0.2,我只想做一个简单的过滤器或使用 where 子语句,我只想在过滤器中执行“and”、“or”和“not”之类的逻辑操作:

val subjectdata = TableQuery[SubjectTable]
...
subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId.userId)).list()

并得到错误:

[error] G:\testprojects\slickplay\app\controllers\ShopController.scala:89: Cannot perform option-mapped operation
[error]       with type: (Long, String) => R
[error]   for base type: (Long, Long) => Boolean
[error]     subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId
.userId)).list()
[error]
                           ^

在 slick 1.0.1 中我可以做到:

val results = Query(TableClass)
.filter(r => r.isNull || r.expires > new Timestamp(DateTime.now().getMillis()))
.list

我想在 Slick2 中的 TableQuery 上做类似的事情。怎么做?

【问题讨论】:

  • 在不知道i.idi.userIdrs.user.get.identifyId 的情况下,很难猜出可能是什么问题。
  • 另外,你在比较两个完全不同的东西。
  • @pedrofurla rs.user.get.identifyId 无关紧要,只是将其视为常量。问题是如何在这里使用和不使用这个逻辑运算符。
  • @user504909 - 编译器报告了一个类型问题,这是 Scala 真正擅长的。特别是,看起来i.id 的类型与id 的类型不同,或者i.userId 的类型与rs.user.get.identityId 的类型不同。

标签: scala slick logical-operators slick-2.0


【解决方案1】:

要知道的是,Slick 的操作对类型的要求比 Scala 的更严格。两个操作数必须具有相同的基本类型,可以选择包装在选项中。因此,将 Double 与 Double 或 Option[Double] 进行比较是可以的,但将其与 Int 进行比较会给您这样的编译时警告。错误消息提示您解决问题

[error] G:\testprojects\slickplay\app\controllers\ShopController.scala:89: Cannot perform option-mapped operation
[error]       with type: (Long, String) => R
[error]   for base type: (Long, Long) => Boolean
[error]     subjectdata.where(i=>(i.id===id && i.userId===rs.user.get.identityId
.userId)).list()

(Long, String) => R 中,您会看到参数没有匹配的类型,并且无法确定返回类型。所以我假设idrs.user.get.identityId 是一个字符串。使用.toInt 变成Int。或者,您可以使用.asColumnOf[String] 转换数据库端值。

【讨论】:

    猜你喜欢
    • 2018-06-19
    • 2018-05-03
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 2015-05-02
    相关资源
    最近更新 更多