0

我正在玩 Play Framework,现在我正在尝试使用简单的表“用户”访问 PostgreSQL 数据库,定义如下:

SELECT * FROM users;
id  | first_name | last_name | email | password 
-c--+------------+-----------+-------+----------
AI  | text       | text      | text  | text

在 Play 中,我有我的用户案例类:

case class User(id: Int, mail: String, pwd: String, firstName: String, lastName: String)

我正在使用以下导入:

import play.api.db.slick.DatabaseConfigProvider
import slick.driver.PostgresDriver
import slick.driver.PostgresDriver._
import slick.lifted.Tag

我可以将我的数据库与以下内容一起使用

@Inject
// Inject Database config provider lib
var dbConfigProvider: DatabaseConfigProvider = _

val dbConfig = dbConfigProvider.get[PostgresDriver]

import dbConfig.driver._

我正在关注此地址上的 Slick 3.0.0 文档:http: //slick.typesafe.com/doc/3.0.0/queries.html#queries。为了建立我的请求,我首先尝试执行以下操作:

class Users(tag: Tag) extends Table[(Int,String,String,String,String)](tag, "users") {
  def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
  def mail = column[String]("email")
  def pwd = column[String]("password")
  def firstName = column[String]("first_name")
  def lastName = column[String]("last_name")
  def * = (id,mail,pwd,firstName,lastName)
}

这并不成功:我的 IDE 说 Expression 不符合预期的类型 ProvenShape,并./activator compile

[...] could not find implicit value for parameter tt: slick.ast.TypedType[Int]

其次,我尝试使用以下链接 slick.typesafe.com/doc/3.0.0/schemas.html#mapped-tables 并写了这个:

class Users(tag: Tag) extends Table[User](tag, "users") {
  def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
  def mail = column[String]("email")
  def pwd = column[String]("password")
  def firstName = column[String]("first_name")
  def lastName = column[String]("last_name")
  def * = (id, mail, pwd, firstName, lastName) <> (User.tupled, User.unapply)
}

但是 IDE 无法识别“<>”符号,加上 unapply 方法中缺少 args...

你对我有什么线索吗?我现在迷路了……

谢谢!

Intellij 错误截图

4

1 回答 1

2

经过几个小时的研究,我终于找到了问题所在。为了解决它,我有:

  • 将 Slick 更新到 build.sbt 中的最新版本:

libraryDependencies ++= Seq( "com.typesafe.play" %% "play-slick" % "2.0.0", "com.typesafe.play" %% "play-slick-evolutions" % "2.0.0" )

  • 导入 api(不仅是驱动程序......)

import dbConfig.driver._ // THIS was the problem import dbConfig.driver.api._ // .api._ is important...

现在一切正常。

于 2016-03-27T19:07:12.647 回答