12

I'm following guidelines of Slick documentation and I don't understand what I'm doing wrong here:

package models

import scala.slick.session.Database
import Database.threadLocalSession
import scala.slick.jdbc.{GetResult, StaticQuery => Q}
import javax.sql.DataSource
import Q.interpolation

object Data {

    case class User(user: String, password: String)

    lazy val db = Database.forName("default")

    def result: Option[User] = {
        db.withSession {
            sql"SELECT user, password FROM user WHERE user = 'user' AND password = 'pass'".as[User]
        }
    }

}

The line

sql"SELECT user, password FROM user WHERE user = 'user' AND password = 'pass'".as[User]

is giving me this:

Multiple markers at this line
    - could not find implicit value for parameter rconv: scala.slick.jdbc.GetResult[models.Data.User]
    - could not find implicit value for parameter rconv: scala.slick.jdbc.GetResult[models.Data.User]

What am I doing wrong here?

Play Framework 2.2.0, Scala 2.10.3, Slick 1.0.1

Caballero
  • 11,546
  • 22
  • 103
  • 163

1 Answers1

14

You need to provide the conversion function from result to user. Copied and adapted straight from the slick home:

implicit val getUserResult = GetResult(r => User(r.<<, r.<<))

Or this section from the documentation you linked

pedrofurla
  • 12,763
  • 1
  • 38
  • 49
  • 1
    But where should this conversion function exist? within the User class? Within the function that is performing the query? somewhere else? That is the question I never see answered. Also every time I try this I get "diverging implicit expansion" error – Eric Hartford Nov 12 '13 at 20:03
  • @EricHartford In my example I've put it inside `Data` object just below `case class User`, don't know if that helps. Also, look up my other questions regarding the same topic - I've got like half a dozen of them solved. – Caballero Nov 13 '13 at 13:46
  • 1
    Doesn't really matter where you put it, as long as it's in scope. In some circumstances Scala automatically imports implicits present in the companion objects, so in the `User` companion object might be a good place. – pedrofurla Nov 13 '13 at 18:36