How to do Scala enum (enumerations) in Slick?

2,151 views
Skip to first unread message

phi...@gmail.com

unread,
Mar 15, 2013, 11:55:29 PM3/15/13
to scala...@googlegroups.com
Hi,

So I would like to express the type of some object with an enum, how can I do this?

Thanks, Philip

Jason Giedymin

unread,
Mar 16, 2013, 10:12:21 AM3/16/13
to scala...@googlegroups.com
See how I use a case class for the slick table type https://github.com/JasonGiedymin/sherpa/blob/master/app/models/Post.scala

-Jason
--
 
---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Shawn

unread,
Mar 20, 2013, 12:47:51 PM3/20/13
to scala...@googlegroups.com
I don't see how this addresses the OP question.

Let me ask the question another way:

How do you persist Enumerations (and objects that have enumerations as data) in Slick?

Jason Giedymin

unread,
Mar 20, 2013, 1:46:10 PM3/20/13
to scala...@googlegroups.com, scala...@googlegroups.com
Database native enum your talking about?

-Jason

Stefan Zeiger

unread,
Mar 20, 2013, 2:09:01 PM3/20/13
to scala...@googlegroups.com
There's a unit test that does something similar: https://github.com/slick/slick/blob/1.0/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/MapperTest.scala#L87

This uses an algebraic data type instead of a Scala Enumeration but the same mechanism could be used for enums.

Shawn

unread,
Mar 20, 2013, 11:00:50 PM3/20/13
to scala...@googlegroups.com
Thanks Stefan, that was much more like it (at least for my question).

In the end ( pardon my scala noob code) I was able to get something like this to work. Given a scala enumeration like:

object EliminationPathway extends Enumeration {
  val Cyp3A4  = Value("3A4")
  val Cyp2D6  = Value("2D6")
  val Cyp2C19 = Value("2C19")
  val Cyp2B6  = Value("2B6")
  val Cyp2C9  = Value("2C9")
  val Cyp1A2  = Value("1A2")
}

I was able to define a mapper to/from this enumeration as follows:

// this maps the EliminationPathway enum to and from the database
  implicit val eliminationPathwayTypeMapper = MappedTypeMapper.base[EliminationPathway.Value, Int](
  // conversion from EliminationPathway to int
  {
    cyp => cyp.id
  },
  // conversion back from int to enum
  {
    id => EliminationPathway(id)
  }
  )

then define columns on a table like:

def majorPathway = column[EliminationPathway.Value]("majorpathway")

nafg

unread,
Apr 4, 2013, 11:02:46 PM4/4/13
to scala...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages