【发布时间】:2015-06-28 21:42:45
【问题描述】:
我在 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$
所以似乎添加伴随对象会破坏我的数据库映射代码。早些时候它在 case 类上寻找元组属性,但现在它在寻找对象并且它没有找到它。
我怎样才能拥有案例类,然后是数据库映射和表单映射?
【问题讨论】:
-
是scala的bug,可以用
(Person. apply _).tupled代替Person. tupled -
这行得通。您应该将此作为答案,以便我可以相应地标记它。
标签: scala playframework slick slick-3.0