1

I am attempting to retrieve a list of user IDs from a database in Scala using ScalikeJDBC. My issue is that the query, when I run it in a database tool, returns many rows. However, when I attempt to retrieve this data as a list in Scala, it contains only one element. Is there something I am missing?

val ids = List(1,2,3,4)
val statement =
  sql"""
      SELECT
       |  id
       |from users
       |where id in (${ids mkString ","})
       ;
    """.stripMargin
NamedDB('aurora) readOnly { implicit session =>
  val list:List[Int] = statement.map(rs => rs.int("id")).list.apply()
  //prints "list size: 1"
  println(s"list size: ${list.size}")
}

ScalikeJDBC docs here.

Brian Risk
  • 1,244
  • 13
  • 23
  • 1
    So you're executing `SELECT id FROM users WHERE id IN (1,2,3,4);` in your DB tool and seeing multiple results? Your code look correct, have you double checked things such as your definitely using same database connection? – timothyclifford Jan 04 '18 at 16:56
  • Also, if your list only contains 1 value, what is it? – timothyclifford Jan 04 '18 at 16:57
  • @timothyclifford, very suspiciously it selects only the first id of the list – Brian Risk Jan 04 '18 at 17:08
  • Is there a reason you're using all the `|` in your select statement? – timothyclifford Jan 04 '18 at 17:17
  • 1
    @timothyclifford those characters are for pretty formatting and are removed by stripMargin(). I have run this without those characters and the result is the same. – Brian Risk Jan 04 '18 at 17:36
  • Brian, to re-state what Timothy asked in the first comment, what happens if you remove the `where` clause altogether? Do you still get only one record? Which one? In other words, do you really have more than 1 record in the subset of `id in (1,2,3,4)` in your DB? – SergGr Jan 04 '18 at 18:28

0 Answers0