Play Scala Anorm parser generates UnexpectedNullableFound, even if the parser is marked as optional

The table is defined as follows:

CREATE TABLE Session ( id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, something varchar(32), PRIMARY KEY (id) ); 

And my request looks like this:

 SQL("SELECT something FROM Session WHERE id={id}").on("id" -> id).as(str("something") ?) 

While this gives the correct type ( Option[String] ) at compile time, it throws a RuntimeException(UnexpectedNullableFound(SESSION.SOMETHING)) at runtime.

For recording, I use Play 1.2.4, Play Scala 0.9.1 and a nested H2 database.

+6
source share
1 answer

The problem is that str("something") ? means you are getting something from the column with an invalid value, but I'm not sure if the row will be there or not. I think you want:

 SQL("SELECT something FROM Session WHERE id={id}").on("id" -> id).as(get[Option[String]]("something") ?).getOrElse(None) 

The SQL as is statement gives us Option[Option[String]] , because we are not sure if the row exists, and if there is a row, we are not sure whether the column is null or not. So we need to do getOrElse to just reduce it to Option[String]

+9
source

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


All Articles