我试图使用一个简单的自定义光滑列示例as described in here。鉴于此" enum":
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实际上可以转换我的&#34; enum&#34;为了串起来,为什么他会抱怨?
P.S。我是非常 Slick的新手(和Scala一样,老实说)。
答案 0 :(得分:1)
按照the docs中的示例,它基本上看起来没有正确设置映射对象。
implicit val entityTypeMapper = MappedColumnType.base[EntityType, String] (
EntityTypeImplicits.toString,
EntityTypeImplicits.fromString)
这应该可以解决这个问题......你基本上需要给两个能够执行这种转换的功能(通过给出下划线,不确定你在做什么?)。我在表类中也有更明确定义的类型信息,但我怀疑这很重要,只是为了帮助排除故障...
def entityType : Rep[EntityType] = column[EntityType]("type")
希望有所帮助!我发现Slick文档有点meh,&amp;谷歌搜索可以引导你进入一个被弃用的语法兔子洞......