Since version 2.3.0 of the anorm library of play framework, the trait Pk
is deprecated and it suggests the usage of its subclasses Id
and NotAssigned
(documentation).
But what if we have a variable that can take either an Id
or a NotAssiged
?
Specifically, in my code I have a class Person(id: Pk[Long], name: String)
. Using Pk
as the type of id
, I can create new users like Person(NotAssigned, "kostas")
or get existing from my db Person(Id(3), "kostas")
.
How can I migrate my code to not use the deprecated Pk
trait, but hold the same functionality?
Pk[A]
is the same as Option[A]
by structure, where Id[A](value)
corresponds to Some[A](value)
, and NotAssigned
corresponds to None
.
So the recommended migration would be to use Option[Long]
, instead. I don't really understand the developers' decision to deprecate Pk[A]
though, but not Id[A]
and NotAssigned
, as both are essentially useless without it. Nonetheless, Option
will function the same for you, and anorm handles it just the same.
Migration notes are being added about this deprecation: https://github.com/playframework/playframework/pull/3029/files . Previous answer is right about Option use.
Best
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With