scalaslick

How to pass an array to a slick SQL plain query?


How to pass an array to a slick SQL plain query? I tried as follows but it fails:

// "com.typesafe.slick" %% "slick" % "3.3.2",  // latest version
val ids = Array(1, 2, 3)
db.run(sql"""select name from person where id in ($ids)""".as[String])

Error: could not find implicit value for parameter e: slick.jdbc.SetParameter[Array[Int]]

However this ticket seems to say that it should work: https://github.com/tminglei/slick-pg/issues/131

Note: I am not interested in the following approach:

db.run(sql"""select name from person where id in #${ids.mkString("(", ",", ")")}""".as[Int])

Solution

  • The issue you linked points to a commit which adds this:

    def mkArraySetParameter[T: ClassTag](/* ... */): SetParameter[Seq[T]]
    def mkArrayOptionSetParameter[T: ClassTag](/* ... */): SetParameter[Option[Seq[T]]]
    

    Note that they are not implicit.

    You'll need to do something like

    implicit val setIntArray: SetParameter[Array[Int]] = mkArraySetParameter[Int](...)
    

    and make sure that is in scope when you try to construct your sql"..." string.