Play Scala Anorm parser throws UnexpectedNullableFound even when the parser is marked as optional

StackOverflow https://stackoverflow.com/questions/9244900

Question

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.

Was it helpful?

Solution

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]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top