6
votes

I have the following postgres column definition:

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

i.e:

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

Should any extra definition go where the ...???... is currently located?

EDIT (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
3

3 Answers

4
votes

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

2
votes
2
votes

Slick 3 Example

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)
}