Query with display type columns in Slick

How could you write this query in SLICK?

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

DateTime.now is of type org.joda.time.DateTime

and _.expirationTime is the mapped column type of the same type.

I get this error

 [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 

right now with a request in this form.

+6
source share
3 answers

My guess is that JodaTime types are not supported by out-of-box types for stains. If you change this column to java.sql.Timestamp and use Timestamp as the comparison value, everything will work. If you want to use joda types in your slippery column mappings, you can look at this:

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

+4
source

Importing com.github.tototoshi.slick.JodaSupport._ fixed the problem for me.

+1
source

What will i do:

Suppose your Token class looks like this and you use 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) } 

Eg. Dependencies:

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

Then you just need to:

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

If you use any other db than mysql, you need to change the import and dependencies.

+1
source

Source: https://habr.com/ru/post/947368/


All Articles