2016-01-23 50 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文檔是一個小問題,&谷歌搜索可以引導你進入一個已過時的語法的兔子洞...