我有一个枚举类型ResourceType,我试图使用光滑的API将其作为Int存储在数据库中。我已经为ResourceType定义了一个自定义类型映射器,但是在我的表定义中我得到了一个编译器错误,说明“找不到匹配的形状.Slick不知道如何映射给定的类型。”。是否有可能使这项工作?
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)
}
答案 0 :(得分:9)
resourceType
列需要包含ResourceType
类型。所以试试
def resourceType = column[ResourceType]("ResourceType")
您可能必须在列定义上方隐式移动类型映射器。