0

I always run into RuntimeException when i invoke following query:

val app = from(appTable)(app => where (app.id === application_ID)
      select(app)).single

This query always throws a RuntimeException:

java.lang.RuntimeException: next called with no rows available

What is wrong with my query?

B. Kemmer
  • 1,517
  • 1
  • 14
  • 32

1 Answers1

4

single means that you expect one and only one result of query. If resultset is empty you receive this exception. Use headOption method instead of single.

Sergii Lagutin
  • 10,561
  • 1
  • 34
  • 43
  • I thought i'll get a 'NoSuchElementException' if it is None. Well i'll try headOption. – B. Kemmer Nov 12 '14 at 09:29
  • 1
    @kemmer `single` returns record `R` but option of record `Option[R]`. – Sergii Lagutin Nov 12 '14 at 09:46
  • 4
    Squeryl 0.9.6 also added [`singleOption`](https://github.com/squeryl/squeryl/blob/master/src/main/scala/org/squeryl/Query.scala#L55) which will more closely mimic the behavior of `single`, ie: if more than one rows are returned - `singleOption` will throw an exception, but otherwise return `Some` or `None` if the results contain 1 or 0 rows respectively. `headOption` is probably the safest guard against an exception overall, but wanted to mention the alternative for completeness. – jcern Nov 12 '14 at 15:04