Jordan
Jordan

Reputation: 1619

PlayFramework - Executing query in DBAction

I'm using Play 2.3.8 and Slick 2.1.0 and according to the docs, I should be able to execute queries in my controller actions like this:

val customers = TableQuery[Customers]

def index = DBAction { implicit request =>
  val result = Json.toJson(customers.list)
  Ok(result)
}

However, when I try this I get an error - no implicit session in scope. I can get around it by doing this:

val customers = TableQuery[Customers]

def index = DBAction { implicit request => 
  implicit val session = request.dbSession
  val result = Json.toJson(customers.list)
  Ok(result)
}

Is this what's required or is there a simpler way? Thanks!

Upvotes: 0

Views: 76

Answers (1)

Matteo Guarnerio
Matteo Guarnerio

Reputation: 724

You can avoid that implicit using this imports (PoC):

import play.api.mvc._
import play.api.Play.current
import play.api.db.slick._
import play.api.db.slick.Config.driver.simple._

...

object Products extends Controller {

  def all = DBAction { implicit rs =>
    Ok(Json.toJson(products.list))
  }

}

Upvotes: 1

Related Questions