0

我有一个案例类UserAccount

case class UserAccount(Id: Option[Long], Name: String, occupation: Occupation)

和一个对象Occupation

sealed trait Occupation

object Occupation {

  case object Teacher extends Occupation
  case object Student extends Occupation
  case object Others extends Occupation

}

我使用 play slick 3 创建了一个模式

class AccountSchema(tag: Tag) extends Table[UserAccount](tag, "user_account") {

  def id = column[Long]("id", O.PrimaryKey, O.AutoInc)

  def name = column[String]("name")

  def occupation = column[Occupation]("occupation")

  def * = (id.?, name, occupation) <>((UserAccount.apply _).tupled, UserAccount.unapply)
}

当我尝试运行该项目时。由于该列,我收到错误Occupation

如何在光滑的表格列中使用对象职业?

4

1 回答 1

1

您需要添加从关系模型到对象模型的映射,反之亦然。

sealed trait Occupation

object Occupation {

  implicit val occupationType = MappedColumnType.base[Occupation, String](
{ occupation => if(occupation==Teacher)  "Teacher" else if (occupation == Student) "Student" else "Others"},
{ occupation => if(occupation == "Teacher") Teacher else if (occupation == "Student") Student else Others }
)
  case object Teacher extends Occupation
  case object Student extends Occupation
  case object Others extends Occupation
}

这里有更多细节:http ://slick.typesafe.com/doc/3.0.0/userdefined.html

于 2015-08-04T16:42:55.683 回答