Ali Salehi
Ali Salehi

Reputation: 6999

Creating instances of model objects with database defaults in Play

I am experimenting with Anorm library of Playframework v2 in Scala. I have a case class like below

case class User (
  id:Pk[Long] = NotAssigned, // auto increment value in mysql   
  name: String
  updated_at: Date // assigned through a trigger by Database
  )

val testUser = new User(NotAssigned:Pk[Long], "Ali", null)

Is it find to use null for fields such as updated_at which their values are assigned directly through DB ? How about using something like Pk[Date] ? I suppose I don't understand the Pk type in Anorm correctly.

I appreciate your comments,

Upvotes: 1

Views: 285

Answers (2)

Fynn
Fynn

Reputation: 4873

I think assigning null values is never a good idea. I'm using a default date instead. You should then omit the default values when constructing new values of type User (the new keyword is unnecessary for Scala's case classes):

case class User (
  id:Pk[Long] = NotAssigned,
  name: String
  updated_at: Date = new Date(0) //1970-01-01
  )

val testUser = User("Ali")

Upvotes: 2

Julien Richard-Foy
Julien Richard-Foy

Reputation: 9663

If this value is automatically set by your database you should not need to set it by yourself. Thus, it should not appear in your write operations.

Upvotes: 1

Related Questions