Scala Play和Slick。编写对象的表单映射和数据库映射

时间:2015-06-28 21:42:45

标签: scala playframework slick slick-3.0

我在Play中使用Slick 3.0。我有这个小类,我已经编写了数据库映射

case class Person(id: Int, firstname: String, lastname: String)

class People(tag: Tag) extends Table[Person](tag, "PEOPLE") {
  def id = column[Int]("PERSON_ID", O.PrimaryKey, O.AutoInc)
  def firstname = column[String]("PERSON_FIRST_NAME")
  def lastname = column[String]("PERSON_LAST_NAME")
  def * = (id, firstname, lastname) <> (Person.tupled, Person.unapply _)
}

这可以完美地编译和工作。现在我创建了一个HTML表单,我将在其中进行数据输入,我需要将HTML表单绑定到Person对象。所以我写了

object Person {
  val form = Form(mapping(
    "id" -> number,
    "firstname" -> text,
    "lastname" -> text
  )(Person.apply)(Person.unapply))
}

但现在我收到错误消息

[error] /Users/abhi/ScalaProjects/MyPlay1/app/tables/PersonDAO.scala:18: value tupled is not a member of object models.Person
[error]   def * = (id, firstname, lastname) <> (Person.tupled, Person.unapply _)
[error]                                                ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 4 s, completed Jun 28, 2015 4:37:50 PM
Mohitas-MBP:MyPlay1 abhi$ 

所以似乎添加一个伴随对象会破坏我的数据库映射代码。之前它正在查找案例类中的tupled属性,但现在它正在查找对象并且它找不到它。

我如何拥有案例类,然后是数据库映射和表单映射?

1 个答案:

答案 0 :(得分:3)

这是scala的错误,您可以使用(Person. apply _).tupled代替Person. tupled作为解决方法