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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With