Can I use Scala List with Slick (Play)?

I am trying to keep a list of integers, here is what I am doing:

MODEL

case class Score( scoresPerTime: List[Int] ) object Scores extends Table[Score]("SCORES"){ def scorePerTime = column[List[Int]]("SCORE_PER_TIME") //...more code } 

controller

 val form = Form( Map( "scoresPerTime" -> list(number) )(Score.apply)(Score.unapply) ) 

I get one compilation error :

 .... could not find implicit value for parameter tm: scala.slick.lifted.TypeMapper[List[Int]][error] def scorePerTime = column[List[Int]]("SCORE_PER_TIME") 

How can I fix this to enter a list? or maybe try another option, like tuple, enum ...

+6
source share
1 answer

You can do this by specifying the mapper type from let say List [Int] to String and vice versa.

One possibility:

 implicit def date2dateTime = MappedTypeMapper.base[List[Int], String]( list => list mkString ",", str => (str split "," map Integer.parseInt).toList ) 

I say this is possible because I have not tested. Not sure if returning the list will cause Slick to fail. One place where it can be ambiguous is aggregated queries in which you want to count the quantity and not do count(field) (which, obviously, will be one).

But this is completely irrelevant. In a relational way, it will have a new table with two fields, one foreign key refers to one row in the SCORES table and another field with one SCORE_PER_TIME . The foreign key must not be a unique index, so the search is quick. And the stain does it pretty well.

+6
source

Source: https://habr.com/ru/post/949341/


All Articles