[英]slick 3 auto-generated - default value (timestamp) column, how to define a Rep[Date] function
I have the following postgres column definition: 我有以下postgres列定义:
record_time TIMESTAMP WITHOUT TIME ZONE DEFAULT now()
How would I map it to slick? 我如何将其映射到光滑? Please take into account that I wish to map the default value generated by the now()
function 请考虑我希望映射now()
函数生成的默认值
ie: 即:
def recordTimestamp: Rep[Date] = column[Date]("record_time", ...???...)
Should any extra definition go where the ...???...
is currently located? 是否有任何额外的定义去...???...
目前位于哪里?
EDIT (1) 编辑(1)
I do not want to use 我不想用
column[Date]("record_time", O.Default(new Date(System.currentTimeMillis()))) // or some such applicative generation of the date column value
I found a blog explaining that you can use the following: 我发现一个博客解释说你可以使用以下内容:
// 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()"))
see: http://queirozf.com/entries/scala-slick-dealing-with-datetime-timestamp-attributes 请参阅: http : //queirozf.com/entries/scala-slick-dealing-with-datetime-timestamp-attributes
I guess this is not supported yet. 我想这还不支持。 Here is issue: https://github.com/slick/slick/issues/214 这是问题: https : //github.com/slick/slick/issues/214
Slick 3 Example 光滑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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.