slick 3 auto-generated - default value (timestamp) 列,如何定义 Rep[Date] 函数

slick 3 auto-generated - default value (timestamp) column, how to define a Rep[Date] function

我有以下 postgres 列定义:

record_time TIMESTAMP WITHOUT TIME ZONE DEFAULT now()

我如何将它映射到 slick?请考虑到我希望映射 now() 函数

生成的默认值

即:

def recordTimestamp: Rep[Date] = column[Date]("record_time", ...???...)

任何额外的定义是否应该放在 ...???... 当前所在的位置?

编辑 (1)

我不想用

column[Date]("record_time", O.Default(new Date(System.currentTimeMillis()))) // or some such applicative generation of the date column value

我想这还不支持。 这是问题:https://github.com/slick/slick/issues/214

我找到了一篇博客,说明您可以使用以下内容:

// slick 3
import slick.profile.SqlProfile.ColumnOption.SqlType
def created = column[Timestamp]("created", SqlType("timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP"))

// slick 3
def createdAt = column[Timestamp]("createdAt", O.NotNull, O.DBType("timestamp default now()"))

参见:http://queirozf.com/entries/scala-slick-dealing-with-datetime-timestamp-attributes

Slick 3 示例

import slick.driver.PostgresDriver.api._
import slick.lifted._
import java.sql.{Date, Timestamp}

/** A representation of the message decorated for Slick persistence
  * created_date should always be null on insert operations.
  * It is set at the database level to ensure time syncronicity
  * Id is the Twitter snowflake id. All columns NotNull unless declared as Option
  * */
class RawMessages(tag: Tag) extends Table[(String, Option[String], Timestamp)](tag, Some("rti"), "RawMessages") {
  def id = column[String]("id", O.PrimaryKey)
  def MessageString = column[Option[String]]("MessageString")
  def CreatedDate = column[Timestamp]("CreatedDate", O.SqlType("timestamp default now()"))
  def * = (id, MessageString, CreatedDate)
}