首页 文章

在Slick中查询类型映射列

提问于
浏览
6

你会如何在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

现在使用此表单中的查询 .

3 回答

  • 4

    我的猜测是JodaTime类型不是开箱即用的支持类型 . 如果您将该列更改为 java.sql.Timestamp 并使用 Timestamp 作为比较值,那么事情就可以了 . 如果要在光滑的列映射中使用joda类型,可能需要查看以下内容:

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

  • 1

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

  • 1

    我会怎么做:

    让我们假设您的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之外的任何其他数据库,则需要更改导入和依赖项 .

相关问题