scalaslickslick-3.0

Scala Slick store only selected fields of the case class


I have 2 case classes

case class PPU(id : String,
           name : String,
           mini_ppus : List[MiniPPU]..)

case class MiniPPU( minPpuId : String,
                ppu_id : String.. )

I have a PPU table and want to store only id and name but not the mini_ppus.

class PpuTable(tag: Tag) extends Table[PPU](tag, None, _tableName ="ppu") {

def * = (id, name) <> (
{ tuple: (String,String) => 
PPU(id.asInstanceOf[String], name.asInstanceOf[String], ListBuffer[MiniPPU]().toList)},
{ ppuTable: PPU => Option(ppuTable.id, ppuTable.name) }
)

   val id = column[String]("ppu_id", O.PrimaryKey)
   val name: Rep[String] = column[String]("name")
}

Whenever im trying to select something from the table, i'm getting an error:

java.lang.ClassCastException: slick.relational.RelationalTableComponent$Table$$anon$1 cannot be cast to java.lang.String

what is the correct way to override def * method ?


Solution

  • Instead of this:

    { tuple: (String,String) => PPU(id.asInstanceOf[String], name.asInstanceOf[String], ListBuffer[MiniPPU]().toList)}
    

    try this:

    { tuple: (String, String) => PPU(tuple._1, tuple._2, ListBuffer[MiniPPU]().toList)}
    

    Use the values from the tuple, not the ones declared before <>