I am newbie to Play Scala and Slick. I am trying to map between Enumeration on Scala to String/Int on Slick. I wrote the following code but I got an error. Could you please let me know how I can fix this.
I am using Scala 2.11.6, Slick 3.1.0, Play 2.4.4, play-slick 1.1.1.
package models
import javax.inject.{Inject, Singleton}
import play.api.db.slick.DatabaseConfigProvider
import slick.driver.JdbcProfile
import scala.concurrent.ExecutionContext
class Color extends Enumeration {
type Color = Value
val Blue = Value("Blue")
val Red = Value("Red")
val Green = Value("Green")
}
object Color extends Color
case class Sample(name:String, id:Int, c:Color)
@Singleton
class ColorDao @Inject()(dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
val dbConfig = dbConfigProvider.get[JdbcProfile]
import dbConfig._
import driver.api._
class SampleTable(tag: Tag) extends Table[Sample](tag, "Sample") {
def name = column[String]("NAME")
def id = column[Int]("ID")
def color = column[Color]("COLOR")
def * = (name, id, color) <> (Sample.tupled, Sample.unapply)
}
def enumStringMapper(enum: Enumeration) = MappedColumnType.base[enum.Value, String](
e => e.toString,
s => enum.withName(s)
)
implicit val colorMapper = enumStringMapper(Color)
}
I got the following error.
[error] /Users/yohei/IdeaProjects/slick3-enum-
mapping/app/models/Sample.scala:30:
could not find implicit value for parameter tt:
slick.ast.TypedType[models.Color]
[error] def color = column[Color]("COLOR")
[error] ^
Please see the below link for whole code.
https://github.com/yohei1126/slick3-enum-mapping
--
Fix with this code.
package models
import javax.inject.{Inject, Singleton}
import models.Bar.Bar
import models.Color.Color
import play.api.db.slick.DatabaseConfigProvider
import slick.driver.JdbcProfile
import scala.concurrent.ExecutionContext
object Color extends Enumeration {
type Color = Value
val Blue = Value("Blue")
val Red = Value("Red")
val Green = Value("Green")
}
case class Sample(name:String, id:Int, c:Color)
@Singleton
class ColorDao @Inject()(dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
val dbConfig = dbConfigProvider.get[JdbcProfile]
import dbConfig._
import driver.api._
class SampleTable(tag: Tag) extends Table[Sample](tag, "Sample") {
def name = column[String]("NAME")
def id = column[Int]("ID")
def color = column[Color]("COLOR")
def * = (name, id, color) <> (Sample.tupled, Sample.unapply)
}
implicit val colorMapper = MappedColumnType.base[Color, String](
e => e.toString,
s => Color.withName(s)
)
}
Here is the fix:
package models
import javax.inject.{Inject, Singleton}
import models.Bar.Bar
import models.Color.Color
import play.api.db.slick.DatabaseConfigProvider
import slick.driver.JdbcProfile
import scala.concurrent.ExecutionContext
object Color extends Enumeration {
type Color = Value
val Blue = Value("Blue")
val Red = Value("Red")
val Green = Value("Green")
}
case class Sample(name:String, id:Int, c:Color)
@Singleton
class ColorDao @Inject()(dbConfigProvider: DatabaseConfigProvider)(implicit ec: ExecutionContext) {
val dbConfig = dbConfigProvider.get[JdbcProfile]
import dbConfig._
import driver.api._
class SampleTable(tag: Tag) extends Table[Sample](tag, "Sample") {
def name = column[String]("NAME")
def id = column[Int]("ID")
def color = column[Color]("COLOR")
def * = (name, id, color) <> (Sample.tupled, Sample.unapply)
}
implicit val colorMapper = MappedColumnType.base[Color, String](
e => e.toString,
s => Color.withName(s)
)
}
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