【问题标题】:Scala Play and Slick. Writing Form Mapping and Database Mapping for an objectScala Play 和 Slick。为对象编写表单映射和数据库映射
【发布时间】: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


【解决方案1】:

这是scala的一个bug,你可以使用(Person. apply _).tupled而不是Person. tupled作为解决方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 2015-12-28
    • 2023-03-19
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 2016-03-23
    相关资源
    最近更新 更多