【问题标题】:Scala Slick filter two conditions - error session is not applicableScala Slick 过滤两个条件 - 错误会话不适用
【发布时间】:2014-08-24 17:00:49
【问题描述】:

我正在使用 Scala 和 Slick,我正在尝试使用两个条件执行简单查询

import JiraData._
import org.scala_tools.time.Imports._

import scala.slick.driver.PostgresDriver.simple._

val today = new DateTime()
val yesterday = today.plusDays(-1)

implicit val session = Database.forURL("jdbc:postgresql://localhost/jira-performance-manager",
    driver = "org.postgresql.Driver",
    user = "jira-performance-manager",
    password = "jira-performance-manager").withSession {
    implicit session =>
        val activeUsers = users.filter(_.active === true)
        for (activeUser <- activeUsers) {
            val activeUserWorkogs = worklogs.filter(x => x.username === activeUser.name && x.workDate === yesterday)
        }
}

但我收到错误:

Error:(20, 95) value === is not a member of scala.slick.lifted.Column[org.scala_tools.time.Imports.DateTime]
 Note: implicit value session is not applicable here because it comes after the application point and it lacks an explicit result type
            val activeUserWorkogs = worklogs.filter(x => x.username === activeUser.name && x.workDate === yesterday)
                                                                                                      ^

这里有什么问题?如何获取按两个条件过滤的结果列表?

【问题讨论】:

    标签: postgresql scala slick


    【解决方案1】:

    scala-tools.time 使用 JodaDateTime。见https://github.com/jorgeortiz85/scala-time/blob/master/src/main/scala/org/scala_tools/time/Imports.scala。 Slick 没有对 Joda 的内置支持。有 Slick Joda 映射器:https://github.com/tototoshi/slick-joda-mapper。或者很容易添加自己:http://slick.typesafe.com/doc/2.1.0/userdefined.html#using-custom-scalar-types-in-queries

    附带说明:类似

    for (activeUser <- activeUsers) {
        val activeUserWorkogs = worklogs.filter(...)
    

    看起来好像走错了方向。它将为每个活动用户运行另一个查询。更好的是对所有活动用户的工作日志使用联接或运行单个累积查询。

    【讨论】:

    • 我明白你的意思——谢谢。但现在我需要先解决问题,然后在需要时才进行优化。顺便说一句,我通过使用单个过滤器然后使用 IF 检查第二个条件来解决这个问题。这段代码很糟糕,但它可以工作。我不明白为什么两个过滤器不起作用。
    猜你喜欢
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    相关资源
    最近更新 更多