Compilation error when querying Enumeration field

198 views
Skip to first unread message

Damian

unread,
Aug 25, 2015, 5:46:44 PM8/25/15
to Slick / ScalaQuery
Hi,
I need help with one problem in Slick when I query field of Enumeration type.
I have table defined as:

class Animals(tag : Tag) extends Table[Animal](tag, "animal") {

  implicit val AnimalTypeMapper = MappedColumnType.base[AnimalType.Value, String](
    s => s.toString,
    s => AnimalType.withName(s)
  )

  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
  def name = column[String]("name")
  def kind = column[AnimalType.Value]("kind")

  def * = (id.?, name, kind) <> (Animal.tupled, Animal.unapply)
}



case class and enumeration defined as:

case class Animal(id : Option[Long], name: String, kind: AnimalType.Value)
object AnimalType extends Enumeration {
  val CAT, DOG = Value
}



When I try to query for animal kind only:

val animals = TableQuery[Animals]
animals.map(_.type).result



The following exception is thrown:
No matching Shape found.
Slick does not know how to map the given types.
Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported type in a Query (e.g. scala List).
Required level: slick.lifted.FlatShapeLevel
Source type: slick.lifted.Rep[db.tables.AnimalType.Value]
Unpacked type: T
Packed type: G


When I query for id or name everything is fine.
Please help me with this one, I think I tried everything but I can't make it work.

Damian

unread,
Aug 26, 2015, 11:20:26 AM8/26/15
to Slick / ScalaQuery
Ok I solved it by adding following code fragment in controller (in place where I was executing query):

  implicit val AnimalTypeMapper = MappedColumnType.base[AnimalType.Value, String](
    s => s.toString,
    s => AnimalType.withName(s)
  )


Reply all
Reply to author
Forward
0 new messages