6

I have an events table with these columns

 _________________________
|___name_____|___time_____|
|            |            |
|____________|____________|

The value of time column is auto generated with the current date at creation time.

Equivalently, I have this case class to represent a row in the table.

case class Event(name: String, time: Option[java.sql.Date] = None)

Now, the problem is that when I try to insert an event with

val event = Event("blah blah blah")
events += event

Slick actually insert ('blah blah blah', null) instead of just ('blah blah blah'), and that makes the time field null.

If I set a constraint on the time column to NOT NULL, then I would get an error for the attempt to insert null.

Of course I can decompose my event object and perform the insert, but I wonder if there is a kind of mapping that tells Slick that this column is auto-generated, like in the case with the auto-increment id column:

def id = column[Long]("id", O.AutoInc)

Update

So I try setting the time column as follow

def time = column[java.sql.Date]("time", O.AutoInc)

and it seems to work.

The document for AutoInc states that it is use for auto increment and auto generated column.

Though I am not sure if this is the correct way to do it.

Khanetor
  • 11,595
  • 8
  • 40
  • 76
  • 1
    It is the correct way to do it. – mohit Oct 20 '15 at 13:03
  • Thanks mohit for the confirmation :) – Khanetor Oct 20 '15 at 14:01
  • What if you want the timestamp to be stored when you create AND when you update the row? – ps0604 Mar 11 '16 at 22:27
  • 1
    That is configured within PostgreSql. Slick only concerns about mapping Scala and RDB tables, and generate sql from Scala code. `O.AutoInc` is just a marker saying that Slick should let the database handle this column. You will want to look at creating function and trigger to update timestamp. – Khanetor Mar 12 '16 at 09:05
  • Looks like hack which breaks model. Autogenerated date means I never want to get null in the field. I want DB to set the field value. So *case class Event(name: String, time: Option[java.sql.Date] = None)* None will appear only once during first insert. I hit the same issue... – Capacytron Mar 31 '17 at 20:11

0 Answers0