我有以下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
我发现一篇博客解释了你可以使用以下方法:
// 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
我想这还不支持。问题如下:https://github.com/slick/slick/issues/214
光滑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)
}