将 slick 3 binding/conversion 对象播放到列 table
Play slick 3 binding/conversion object to column table
我有一个案例 class 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
列,我收到错误消息
如何在 slick Table 列使用对象 Occupation?
您需要添加从关系模型到对象模型的映射,反之亦然。
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
我有一个案例 class 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
如何在 slick Table 列使用对象 Occupation?
您需要添加从关系模型到对象模型的映射,反之亦然。
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