简体   繁体   English

slick 3自动生成 - 默认值(timestamp)列,如何定义Rep [Date]功能

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

相关问题 如何在插入新行后立即将自动生成的主键值保存到第二列 - How to save auto-generated primary key value into second column right after inserting new row 如何在PreparedStatement中使用自动生成的@Id? - How to use auto-generated @Id in PreparedStatement? 在PostGres中插入NULL值后,如何自动生成ID? - How do I get my IDs auto-generated when a NULL value is inserted in PostGres? 允许使用简单的字符串值设置自动生成的列 - Allow auto-generated columns to be set with a simple string value 更改自动生成的序列 - Alter auto-generated sequence 如何在同一行发布中使用自动生成的主键 - How to use the auto-generated primary key in the same row postrges PostgreSQL上的流利的nhibernate:将默认列值定义为当前UTC日期? - fluent nhibernate on postgresql: define default column value to current UTC date? 如何在liquibase中使用一些默认时间戳更新日期列 - how to update date column with some default timestamp in liquibase 如何使用epoch的默认时间戳值创建postgres列? - How to create a postgres column with a default timestamp value of epoch? 如何通过Ruby PostgreSQL gem获取自动生成的行主键? - How to get auto-generated row primary key via Ruby PostgreSQL gem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM