[英]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 zone
的timestamp 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.