2015-08-03 18 views
5

我有以下的Postgres列定義:光滑3自動生成的 - 默認值(時間戳)列,如何定義代理商[日期]功能

record_time TIMESTAMP WITHOUT TIME ZONE DEFAULT now() 

我怎麼會它映射到華而不實?請考慮,我要映射由now()函數生成的默認值帳戶

即:

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

如果有任何額外的定義去那裏的...???...當前所在?

EDIT(1)

我不想使用

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

回答

3

我發現了一個博客,說明您可以使用以下方法:

// 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

+0

參考博客,但它是用於slick2。 slick3方式,同樣來自博客,是:'import slick.profile.SqlProfile.ColumnOption.SqlType' 'def created = column [Timestamp](「created」,SqlType(「timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP」) )' – user2829759

2

油滑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) 
} 
+0

太棒了,可以爲映射表做些什麼? – Sergey

+0

如果它在PostgreSQL中被定義爲一個表,是的。如果這是一個視圖,你可能需要像下面這樣的東西:'slick.jdbc.meta.MTable.getTables(None,None,None,Some(Seq(「VIEW」,「TABLE」)))' 上面的例子[從](https://stackoverflow.com/questions/28077228/scala-slick-database-views) –

相關問題