slickslick-3.0

Slick 3 for scala update query not functioning


First of I would like to state that I am new to slick and am using version 3.1.1 . I been reading the manual but i am having trouble getting my query to work. Either something is wrong with my connection string or something is wrong with my Slick code. I got my config from here http://slick.typesafe.com/doc/3.1.1/database.html and my update example from here bottom of page http://slick.typesafe.com/doc/3.1.1/queries.html . Ok so here is my code

Application Config.

mydb= {
  dataSourceClass = org.postgresql.ds.PGSimpleDataSource
  properties = {
    databaseName = "Jsmith"
    user = "postgres"
    password = "unique"
  }
  numThreads = 10
}

My Controller -- Database table is called - relations

package controllers
import play.api.mvc._
import slick.driver.PostgresDriver.api._

class Application extends Controller {

  class relations(tag: Tag) extends Table[(Int,Int,Int)](tag, "relations") {
    def id = column[Int]("id", O.PrimaryKey)
    def me = column[Int]("me")
    def following = column[Int]("following")
    def * = (id,me,following)

  }

  val profiles = TableQuery[relations]

  val db = Database.forConfig("mydb")


  try {
    // ...
  } finally db.close()




  def index = Action {

    val q = for { p <- profiles if p.id === 2 } yield p.following
    val updateAction = q.update(322)
    val invoker = q.updateStatement
    Ok()
  }

}

What could be wrong with my code above ? I have a separate project that uses plain JDBC and this configuration works perfectly for it

db.default.driver=org.postgresql.Driver
db.default.url="jdbc:postgresql://localhost:5432/Jsmith"
db.default.user="postgres"
db.default.password="unique"

Solution

  • You did not run your action yet. db.run(updateAction) executes your query respectively your action on a database (untested):

    def index = Action.async {
      val q = for { p <- profiles if p.id === 2 } yield p.following
      val updateAction = q.update(322)
    
      val db = Database.forConfig("mydb")
      db.run(updateAction).map(_ => Ok())
    }
    

    db.run() returns a Future which will be eventually completed. It is then simply mapped to a Result in play.

    q.updateStatement on the other hand just generates a sql statement. This can be useful while debugging.