Custom mapped column types in the exact table definition

I have an enumerated type ResourceType that I am trying to save to the database as Int using the slick API. I defined a custom mapper type for ResourceType, but I get a compiler error in my definition * in the definition of my table, which says "No shape match found. Slick does not know how to map these types." Can this work be done?

import scala.slick.driver.H2Driver.simple._

case class Resource(val id : Option[Int], val creationDate : Date, val title : String, val resourceType : ResourceType, val description : String) {
}

case class ResourceType private(val databaseCode : Int, val label : String) {
}

object ResourceType {
  val lessonPlan = new ResourceType(1, "Lesson Plan")
  val activity = new ResourceType(2, "Activity")

  val all = scala.collection.immutable.Seq(lessonPlan, activity)

  private val _databaseCodeMap = all.map(t => t.databaseCode -> t).toMap

  def apply(databaseCode : Int) = _databaseCodeMap(databaseCode)
}

class ResourceTable(tag : Tag) extends Table[Resource](tag, "Resource") {
  def id = column[Option[Int]]("ID", O.PrimaryKey, O.AutoInc)
  def creationDate = column[Date]("CreationDate")
  def title = column[String]("Title")
  def resourceType = column[Int]("ResourceType")
  def description = column[String]("Description")

  implicit val resourceTypeTypeMapper = MappedColumnType.base[ResourceType, Int](_.databaseCode, ResourceType(_))

  //Compile error on this line
  def * = (id, creationDate, title, resourceType, description) <> (Resource.tupled, Resource.unapply)
}
+4
source share
1 answer

The column resourceTypemust be of type resourceType. Therefore try

def resourceType = column[ResourceType]("ResourceType")

You may need to move the mapper type, implicit, over the column definition.

+9
source

All Articles