【发布时间】:2016-05-07 06:25:20
【问题描述】:
我一直在尝试使用 slick 3.1.0 中的特征定义映射表。由于官方文档中没有提到任何内容,我什至不确定这是否可能。这是我目前所拥有的:
表定义:
class PersonTable(tag: Tag) extends Table[PersonModel](tag, "person") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def firstName = column[String]("first_name", O.Length(PersonDb.FirstNameColumnLength))
def lastName = column[String]("last_name", O.Length(PersonDb.LastNameColumnLength))
def * = (id.?, firstName, lastName) <> (PersonModelImpl.tupled, PersonModelImpl.unapply _)
}
人物模型:
trait PersonModel {
def id: Option[Int]
def firstName: String
def lastName: String
}
PersonModelImpl:
case class PersonModelImpl(
override val id: Option[Int],
override val firstName: String,
override val lastName: String)
extends PersonModel
编译上面的代码会报错:
Compilation error[type mismatch;
found : slick.lifted.MappedProjection[models.PersonModelImpl,(Option[Int], String, String]
required: slick.lifted.ProvenShape[models.PersonModel]]
但是,在表定义中将 ...extends Table[PersonModel]... 更改为 ...extends Table[PersonModelImpl]... 可以正常工作。
所以基本上我的问题是:
- 是否可以在映射表中使用特征作为
TableElementType? - 如果是,我做错了什么?
【问题讨论】: