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

1

6

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.

Glioma answered 11/2, 2012 at 23:14 Comment(0)
S
9

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]

Schiller answered 29/2, 2012 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.