[2.1.0-scala] Failing to convert MySQL count() to Boolean

72 views
Skip to first unread message

Brian Ross

unread,
Mar 19, 2013, 8:00:07 PM3/19/13
to play-fr...@googlegroups.com
I'm attempting to write a simple query to find out if a User exists but I'm not sure where to go to get the result (Long) to convert to a Boolean using MySQL. Trying to co-opt the Anorm used in the zentasks project for my purposes.

/**
 * Check if a User-email exists
 */
def exists(email: String): Boolean = {
    DB.withConnection { implicit connection =>
      SQL("select count(users.id) = 1 from users where email = {email}"
    ).on(
      'email -> email
    ).as(
      scalar[Boolean].single
    )
  }
}

[RuntimeException: TypeDoesNotMatch(Cannot convert 1:class java.lang.Long to Boolean for column ColumnName(.count(users.email) = 1,Some(count(users.email) = 1)))]

Not sure where to focus on how to solve this, the conversion or is the MySQL result different than H2?

Thanks,
Brian

Brian Ross

unread,
Mar 20, 2013, 1:30:29 PM3/20/13
to play-fr...@googlegroups.com
This seems to work, but seems a bit hackish...is there a more elegant way?

/**
 * Check if a User-email exists
 */
def exists(email: String): Boolean = {
    DB.withConnection { implicit connection =>
      SQL("select count(users.id) = 1 from users where email = {email}"
    ).on(
      'email -> email
    ).as(
      scalar[Long].single
    ) > 0
Reply all
Reply to author
Forward
0 new messages