Description of additional fields in Slick

Slick DSL provides two ways to create additional fields in tables.

In this case, the class:

case class User(id: Option[Long] = None, fname: String, lname: String) 

You can create a table mapping in one of the following ways:

 object Users extends Table[User]("USERS") { def id = column[Int]("id", O.PrimaryKey, O.AutoInc) def fname = column[String]("FNAME") def lname = column[String]("LNAME") def * = id.? ~ fname ~ lname <> (User, User.unapply _) } 

and

  object Users extends Table[User]("USERS") { def id = column[Option[Long]]("id", O.PrimaryKey, O.AutoInc) def fname = column[String]("FNAME") def lname = column[String]("LNAME") def * = id ~ fname ~ lname <> (User, User.unapply _) } } 

What is the difference between the two? Is this path old and the other new, or do they serve different purposes?

I prefer the second option when you define the identifier as optional as part of the identifier definition, because it is more consistent.

+4
source share
1 answer

The operator .? in the first one, you can postpone the choice of having your field optional until the moment your forecasts are determined. Sometimes this is not what you want, but defining PK as Option is probably a little ridiculous, because you can expect PK to be NOT NULL .

Can you use .? in additional projections, except * , for example:

 def partial = id.? ~ fname 

Then you could do Users.partial.insert(None, "Jacobus") and not worry about which fields you are not interested in.

+5
source

All Articles