0
votes

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")
    }

    Ok("hello")
  }

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: https://github.com/typesafehub/activator-hello-slick/blob/master/src/main/scala/CaseClassMapping.scala

What am I missing?

1

1 Answers

0
votes

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._
  db.run(ApiKeys.apiKeys += ApiKey(key = "boo"))

  Ok("Hallo")
}

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