【问题标题】:Querying with type mapped columns in Slick在 Slick 中使用类型映射列进行查询
【发布时间】:2013-06-15 15:17:02
【问题描述】:

您将如何在 SLICK 中编写此查询?

DB.withSession  {
  implicit session =>
    Tokens.where(_.expirationTime < DateTime.now ).delete
}

DateTime.now 是 org.joda.time.DateTime 类型

和 _.expirationTime 是相同类型的类型映射列。

我收到此错误

[error] UserService.scala:80: value < is not a member of scala.slick.lifted.Column[org.joda.time.DateTime]
[error]         Tokens.where(_.expirationTime < DateTime.now ).delete
[error]                                       ^
[error] one error found 

现在使用这种形式的查询。

【问题讨论】:

    标签: scala slick


    【解决方案1】:

    我的猜测是 JodaTime 类型不是开箱即用的 slick 类型。如果您将该列更改为java.sql.Timestamp 并使用Timestamp 作为比较值,则一切正常。如果您想在光滑的列映射中使用 joda 类型,您可能需要查看以下内容:

    https://github.com/tototoshi/slick-joda-mapper

    【讨论】:

      【解决方案2】:

      导入com.github.tototoshi.slick.JodaSupport._ 为我解决了这个问题。

      【讨论】:

        【解决方案3】:

        我会做什么:

        假设您的 Token 类看起来像这样并且您使用 mysql:

        import org.joda.time.DateTime
        import com.github.tototoshi.slick.JdbcJodaSupport._
        import play.api.db.slick.Config.driver.simple._
        
        case class Token(
                          id: Option[Long]
                          expirationTime: Option[DateTime]                  
                         )
        
        class Tokens(tag: Tag) extends Table[Token](tag, "Token") {
          def id = column[Long]("id", O.PrimaryKey, O.AutoInc, O.Nullable)
          def expirationTime = column[DateTime]("expirationTime", O.Nullable)  
        
          def * = (id.?, expirationTime.?) <> (Token.tupled, Token.unapply)
        
        }
        

        例如。依赖:

        libraryDependencies ++= Seq(
          "com.typesafe.slick" %% "slick" % "2.1.0-M2",
          "com.typesafe.play" %% "play-slick" % "0.8.0-M1",
          "com.github.tototoshi" %% "slick-joda-mapper" % "1.2.0",
          "mysql" % "mysql-connector-java" % "5.1.26"
        )
        

        那么你需要做的就是:

        import com.github.tototoshi.slick.JdbcJodaSupport._
        
        
        DB.withSession  {
          implicit session =>
            Tokens.where(_.expirationTime < DateTime.now ).delete
        }
        

        如果您使用 mysql 以外的任何其他 db,则需要更改导入和依赖项。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-03-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多