简体   繁体   English

Slick-将Long隐式转换为java.sql.Timestamp

[英]Slick - implicit conversion of Long to java.sql.Timestamp

Slick 2.1 Scala 2.11.7 Slick 2.1 Scala 2.11.7

Cannot get implicit conversions for Long <-> java.sql.Timestamp or java.sql.Date working. 无法获得Long <-> java.sql.Timestamp或java.sql.Date工作的隐式转换。 I have them working for other conversions, such as JodaTime.DateTime <-> java.sql.Timestamp... 我让他们为其他转换工作,例如JodaTime.DateTime <-> java.sql.Timestamp ...

For example, error returned on query: 例如,查询返回错误:

org.postgresql.util.PSQLException: ERROR: operator does not exist: 
timestamp with time zone >= bigint Hint: No operator matches 
the given name and argument type(s). You might need to add explicit type casts. 
Position: 325

These are the implicits and they are definitely imported properly to be in scope: 这些是隐式的,它们肯定可以正确地导入范围内:

implicit val LongToTimestamp = MappedColumnType.base[Long, Timestamp](
longTS => new Timestamp(longTS),
sqlTS => sqlTS.getTime
)

implicit val LongToDate = MappedColumnType.base[Long, java.sql.Date](
longTS => new java.sql.Date(longTS),
sqlDate => sqlDate.getTime
)

I have also tried using java.lang.Long instead: 我也尝试使用java.lang.Long代替:

implicit val LongToTimestamp = MappedColumnType.base[java.lang.Long, Timestamp](
longTS => new Timestamp(longTS),
sqlTS => sqlTS.getTime
)

implicit val LongToDate = MappedColumnType.base[java.lang.Long, java.sql.Date](
longTS => new java.sql.Date(longTS),
sqlDate => sqlDate.getTime
)

What am I missing here? 我在这里想念什么? Thank you! 谢谢!

-- -

additional info: 附加信息:

The query method is defined as (queued is a java.sql.Timestamp) : 查询方法定义为(排队为java.sql.Timestamp):

def getJobs(jobTypeId: Short, onOrAfter: DateTime): Seq[JobRow] = {
 val q1 = for{j <- Jobs if j.jobTypeId === jobTypeId && j.queued >= onOrAfter.getMillis} yield j
 db.withSession{ implicit s =>
   q1.buildColl[Seq]
 }
}

The table definition: 表定义:

class Jobs(tag: Tag) extends Table[JobRow](tag, "job") {

def jobId = column[Long]("job_id", O.PrimaryKey, O.NotNull)

def jobTypeId = column[Short]("job_type_id", O.NotNull)

def queued = column[Long]("queued", O.NotNull)

def * = (jobId, jobTypeId, queued) <>((JobRow.apply _).tupled, JobRow.unapply_ )

def pk = primaryKey("job_pk", jobId)
}

Your problem here is that the underlying queued column in Postgres is not a bigint but a timestamp with time zone . 您的问题是Postgres中的底层queued列不是bigint而是timestamp with time zonetimestamp with time zone However, in your Jobs table definition for Slick you say that the column is a Long . 但是,在Slick的Jobs表定义中,您说该列是Long Slick already knows how to bind a Long to a query, so it never tries to use your implicit to convert the Long into another type that it does know how to bind. Slick已经知道如何将Long绑定到查询,因此它从未尝试使用您的隐式将Long转换为它确实知道如何绑定的另一种类型。

Ideally you would just use a sql.Timestamp as your Jobs.queued column type (which is what your comment on your query snippet seems to indicate you think it is): 理想情况下,您只需要使用sql.Timestamp作为Jobs.queued列类型(这是您对查询代码段的注释似乎表明您认为是):

def queued = column[Timestamp]("queued", O.NotNull)
// As opposed to your example where this is
// def queued = column[Long]("queued", O.NotNull)

Or, you can create a Slick wrapper for the to_timestamp(double) : 或者,您可以为to_timestamp(double)创建一个Slick包装器:

val toTimestamp = SimpleFunction.unary[Double, Timestamp]("to_timestamp")
val q1 = for {
  j <- Jobs
  if j.jobTypeId === jobTypeId && j.queued >= toTimestamp(onOrAfter.getMillis)
} yield j

Finally, you can add an automatic cast to your Postgres DB to convert. 最后,您可以向Postgres DB添加自动转换以进行转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.sql.Timestamp错误的时间解析 - java.sql.Timestamp wrong time parsing Joda DateTime到java.sql.Timestamp忽略时区? - Joda DateTime to java.sql.Timestamp ignore timezone? 如何在java.sql.Timestamp中使用Joda-Time - How to use Joda-Time with java.sql.Timestamp 如何让PostgreSQL将所有java.sql.Timestamp都视为位于UTC“区域”中? - How make PostgreSQL consider all java.sql.Timestamp as being in the UTC “zone”? Java,Spring:在为java.sql.Timestamp添加时区之后,查找给定时间范围内的对象无法正常工作 - Java, Spring : Finding objects within a given time-frame not working after adding time-zone to java.sql.Timestamp postgres 中的时间戳转换 SQL - Timestamp conversion in postgres SQL Rails / Postgres:警告:隐式转换丢失 integer 精度:'long' 到 'int' - Rails / Postgres: warning: implicit conversion loses integer precision: 'long' to 'int' Slick:找不到参数 e 的隐式值:slick.jdbc.SetParameter[Option[java.util.UUID]] - Slick: could not find implicit value for parameter e: slick.jdbc.SetParameter[Option[java.util.UUID]] rails db schema sql格式TypeError:没有将nil隐式转换为String - rails db schema sql format TypeError: no implicit conversion of nil into String 没有可用的隐式会话Scala Slick - No Implicit session available Scala Slick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM