Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Scala Anorm parser throws UnexpectedNullableFound even when 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 query 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 causes a RuntimeException(UnexpectedNullableFound(SESSION.SOMETHING)) at runtime.

For the record, I'm using Play 1.2.4, Play Scala 0.9.1 and the bundled H2 database.

like image 785
Teo Klestrup Röijezon Avatar asked Feb 11 '12 23:02

Teo Klestrup Röijezon


1 Answers

The issue is that str("something") ? means get from the not-nullable column "something" but I'm not sure if there will be a row or not. I think what you want is:

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

The SQL statement as is gives us an Option[Option[String]], because we're not sure if the row exists, and if the row is there, we're not sure if the column is null or not. That's why we need to do a getOrElse to just reduce it to an Option[String]

like image 172
thatsmydoing Avatar answered Oct 13 '22 01:10

thatsmydoing