1

I want to use play-slick 1.0.0 with play 2.4.0. According to the sample (https://github.com/playframework/play-slick/tree/master/samples/basic), I defined the UserTable like this:

package tables

import models.User
import scala.slick.driver.JdbcProfile

trait UserTable {
  protected val driver: JdbcProfile
  import driver.api._
  class Users(tag: Tag) extends Table[User](tag, "users"){
    def ID = column[Long]("id", O.PrimaryKey, O.AutoInc)
    def email = column[String]("email", O.NotNull)
    def password = column[String]("password", O.NotNull)
    def * = (ID, email, password) <> (User.tupled, User.unapply)
  }
}

And I implemented the controller as follow:

package controllers

import play.api._
import play.api.mvc._
import play.api.db.slick.DatabaseConfigProvider
import play.api.db.slick.HasDatabaseConfig
import tables.UserTable
import scala.slick.driver.JdbcProfile
import play.api.libs.concurrent.Execution.Implicits.defaultContext

import models.User
import tables.UserTable

class UserController extends Controller with UserTable with HasDatabaseConfig[JdbcProfile]{
  val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
  import driver.api._

  val Users = TableQuery[Users]

  def index = Action.async {
    db.run(Users.result).map(res => Ok(views.html.User.index(res.toList)))
  }
}

But, I ran the application and invoked this controller, I got the error

[SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: users)]

How can I create the "users" table?

Michael Zajac
  • 55,144
  • 7
  • 113
  • 138
garasubo
  • 85
  • 7

1 Answers1

1

You need to either use an existing users table or create a table through slick.

Please see http://slick.typesafe.com/doc/3.0.0/gettingstarted.html#populating-the-database

You need to run db.run(Users.schema.create) in your application.

Biswanath
  • 9,075
  • 12
  • 44
  • 58
  • I called db.run(Users.schema.create) in Global.onStart, but it occurred another error: `play.api.PlayException: Cannot init the Global object[null] ` I also tried called it in the UserController constructor, but it failed with `ProvisionException: Unable to provision, see the following errors: 1) Error injecting constructor, java.lang.NullPointerException at controllers.UserController.(UserController.scala:13)` – garasubo Jun 17 '15 at 13:07