1
我想使用一個簡單的自定義光滑的列示例,as described in here。鑑於這種 「枚舉」:斯卡拉浮油3.1簡單列<->案例類映射器
trait EntityType
object EntityTypeImplicits {
implicit def fromString(s: String): EntityType = s match {
case "external" => ExternalEntity()
case "user" => UserEntity()
case "packet" => PacketEntity()
case "share" => ShareEntity()
case _ => throw new RuntimeException("Unknown entity type")
}
implicit def toString(e: EntityType): String = e match {
case ExternalEntity() => "external"
case UserEntity() => "user"
case PacketEntity() => "packet"
case ShareEntity() => "share"
}
}
case class ExternalEntity() extends EntityType
case class UserEntity() extends EntityType
case class PacketEntity() extends EntityType
case class ShareEntity() extends EntityType
case class Entity(identity: String, entityType: EntityType)
與此架構配置
trait Schema extends HasDatabaseConfigProvider[JdbcProfile] {
import driver.api._
implicit val entityTypeMapper =
MappedColumnType.base[EntityType, String] _
implicit val tt: TypedType[EntityType]
class Entities(tag: Tag) extends Table[Entity](tag, "entities") {
def identity = column[String]("identity", O.PrimaryKey)
def entityType = column[EntityType]("type")
def * = (identity, entityType) <>
(Entity.tupled, Entity.unapply)
}
}
這給出了一個編譯錯誤:
[error] No matching Shape found.
[error] Slick does not know how to map the given types.
[error] Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported type in a Query (e.g. scala List).
[error] Required level: slick.lifted.FlatShapeLevel
[error] Source type: (slick.lifted.Rep[String], slick.lifted.Rep[domain.EntityType])
[error] Unpacked type: (String, domain.EntityType)
[error] Packed type: Any
[error] def * = (identity, entityType) <>
[error] ^
我不明白 - 我提供的映射規則,所以Slick實際上可以將我的「枚舉」轉換爲字符串並返回,爲什麼他會抱怨?
p.s.我是非常新斯利克(和斯卡拉,說實話)。