scala - mapping an integer to an object in slick -
i using slick 3 scala , play , wondering how can transform, in table definition, id object.
let's have list of values similar enum in java , id database must correspond id defined in enum. it's similar having table in database referenced through id.
object x extends table[(int, string)]("x") { ... def typeid = column[int]("type_id") def type = ... (aaa or bbb) // filled in type_id ... }
in slick can define own projections (instead of usual apply/unapply of case classes) data in database objects , again. can use string define type in database.
your types might like:
sealed trait x case class aaa() extends x case class bbb() extends x
then slick mapping this:
object xs extends table[x]("x") { def typename = column[string]("type_name") def tox(from: (string)): x = match { case ("aaa") => aaa case ("bbb") => bbb } def fromx(x: x): (string) = x match { case aaa => ("aaa") case bbb => ("bbb") } override def * = (typeid) <> (tox, fromx) }
Comments
Post a Comment