Scala Play 和 Slick。为对象编写表单映射和数据库映射
Scala Play and Slick. Writing Form Mapping and Database Mapping for an object
我在 Play 中使用 Slick 3.0。我有这么小的class,我已经为它编写了数据库映射
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$
所以似乎添加伴生对象破坏了我的数据库映射代码。早些时候它在案例 class 上寻找元组 属性,但现在它正在寻找对象,但没有找到它。
我怎样才能得到案例 class 然后是数据库映射和表单映射?
这是 scala 的一个错误,您可以使用 (Person. apply _).tupled
而不是 Person. tupled
作为解决方法
我在 Play 中使用 Slick 3.0。我有这么小的class,我已经为它编写了数据库映射
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$
所以似乎添加伴生对象破坏了我的数据库映射代码。早些时候它在案例 class 上寻找元组 属性,但现在它正在寻找对象,但没有找到它。
我怎样才能得到案例 class 然后是数据库映射和表单映射?
这是 scala 的一个错误,您可以使用 (Person. apply _).tupled
而不是 Person. tupled
作为解决方法