2016-01-23 36 views
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.我是非常新斯利克(和斯卡拉,说实话)。

回答

1

按照the docs中的示例,它基本上看起来像您没有正确设置您的映射对象。

implicit val entityTypeMapper = MappedColumnType.base[EntityType, String] (
     EntityTypeImplicits.toString, 
     EntityTypeImplicits.fromString) 

这应该做的伎俩......基本上你需要给能够执行这种转换的两个函数(不知道你是在你的情况给予强调做什么?)。我也有表类更加明确的类型信息,但我怀疑这事,只是一些额外的故障排除,以帮助...

def entityType : Rep[EntityType] = column[EntityType]("type") 

希望帮助!我发现Slick文档是一个小问题,&谷歌搜索可以引导你进入一个已过时的语法的兔子洞...