Shurik Agulyansky Shurik Agulyansky - 8 months ago 56
Scala Question

Starting with Slick in Play 2.5 (scala)

I am just starting to dig into Slick with Play Scala (transitioning from Anorm) and immediately facing startup issues with queries.

I have a table class defined as follows:

package model

import slick.driver.PostgresDriver.api._
import slick.lifted.{TableQuery, Tag}

case class ApiKey(id: Option[Int] = None, key: String)

object ApiKeys {
val apiKeys: TableQuery[ApiKeys] = TableQuery[ApiKeys]

class ApiKeys(tag: Tag) extends Table[ApiKey](tag, "api_key"){
def id = column[Int]("id", O.PrimaryKey, O.AutoInc) // This is the primary key column
def key = column[String]("key")

override def * = (id.?, key) <> (ApiKey.tupled, ApiKey.unapply)

Note: The table is already created in db

In controller, for trial, I am trying to insert value:

def index = Action {
db.withConnection { implicit connection =>
ApiKeys.apiKeys += ApiKey(key = "boo")


In here I am getting an error:

value += is not a member of slick.lifted.TableQuery[model.ApiKeys]

However, all the docs that I am reading proposing to do exactly that.
One of the examples here:

What am I missing?


Try with importing import driver.api._ in the controller where you want to use it. For me this would work:

def test = Action {
  import driver.api._ += ApiKey(key = "boo"))


Even though it helps a lot putting the queries all into Dao, so that the DB-Config stays in certain parts of the application.