15

I'm writing Playframework 2.0 application using Scala and Anorm to access db.

Currently I'm using Pk[Long] for id fields and I'm worry about additional get call needed to access actual value. So I start using plain Long for id fields and everything still work perfect.

What Pk is for, and should I use it instead of plain types? Does Pk gives me additional features/benefits over plain types?

lambdas
  • 3,990
  • 2
  • 29
  • 54

1 Answers1

18

Pk allows you to specify a typed primary key.

Also, say you have a contrived model like this:

case class MyModel(id: Pk[Long], foo: String)

You have the benefit of constructing an instance of your model with:

MyModel(anorm.NotAssigned, "notKnownAtRuntime")

...if your database is responsible for generating your keys, or otherwise:

MyModel(anorm.Id(123L), "knownAtRuntime")
opyate
  • 5,388
  • 1
  • 37
  • 64
  • I know about `NotAssigned`, it is handy, since id generates automatically. I just thought there is something else. Thank you anyway! – lambdas Jun 24 '12 at 02:53
  • 4
    Why not using `Option[Long]` and then initialize your Objects with `None` or `Some(123L)`? –  Jun 17 '13 at 12:34
  • 2
    @mklemenz because ```case class MyModel(id: Pk[Long], id2: Pk[Long])``` should not compile where ```case class MyModel(id: Option[Long], id2: Option[Long])``` compiles – Guillaume Massé Nov 21 '13 at 01:14