Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle anorm's Pk deprecation

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?

like image 727
kwstas Avatar asked Jun 12 '14 19:06

kwstas


2 Answers

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.

like image 133
Michael Zajac Avatar answered Nov 12 '22 06:11

Michael Zajac


Migration notes are being added about this deprecation: https://github.com/playframework/playframework/pull/3029/files . Previous answer is right about Option use.

Best

like image 42
cchantep Avatar answered Nov 12 '22 06:11

cchantep