在Slick中,如何为表实现多个投影?

时间:2015-04-30 06:42:12

标签: scala playframework slick slick-2.0

以下是代码(我使用Slick 2.1):

case class UserRecord(id: Long,
                             mID: String,
                             userName: Option[String],
                             firstName: Option[String],
                             lastName: Option[String],
                             fullName: Option[String],
                             email: String,
                             avatarUrl: Option[String],
                             createTime: Timestamp,
                             updateTime: Timestamp,
                             status: Int,
                             socialProviders: Int)

  case class UserProfile (
      userName: String,
      firstName: Option[String],
      lastName: Option[String],
      fullName: Option[String],
      email: String,
      avatarURL: Option[String])


class UserTable(tag: Tag) extends Table[UserRecord](tag, "User") {
  def id = column[Long]("id", O.AutoInc)

  def mID = column[String]("mID", O.PrimaryKey)

  def username = column[Option[String]]("username")

  def firstName = column[Option[String]]("firstname")

  def lastName = column[Option[String]]("lastname")

  def fullName = column[Option[String]]("fullname")

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

  def avatarUrl = column[Option[String]]("avataurl")

  def createTime = column[Timestamp]("createTime")

  def updateTime = column[Timestamp]("updateTime")

  def status = column[Int]("status")

  def socialProviders = column[Int]("socialProviders")

  def * = (id, mID, username, firstName, lastName, fullName,
    email, avatarUrl, createTime, updateTime, status, socialProviders) <>(UserRecord.tupled, UserRecord.unapply _)

  def profile = (username, firstName, lastName, fullName, email, avatarUrl) <> (UserProfile.tupled, UserProfile.unapply _)
}

我尝试在Table类中创建两个映射*profile,但是,Slick抱怨这个:

[error] Slick does not know how to map the given types.
[error] Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported type in a Query (e.g. scala List).
[error]   Required level: scala.slick.lifted.FlatShapeLevel
[error]      Source type: (scala.slick.lifted.Column[Option[String]], scala.slick.lifted.Column[Option[String]], scala.slick.lifted.Column[Option[String]], scala.slick.lifted.Column[Option[String]], scala.slick.lifted.Column[String], scala.slick.lifted.Column[Option[String]])
[error]    Unpacked type: (String, Option[String], Option[String], Option[String], String, Option[String])
[error]      Packed type: Any
[error]   def profile = (username, firstName, lastName, fullName, email, avatarUrl) <> (UserProfile.tupled, UserProfile.unapply _)
[error]                                                                             ^

我看到了blog这个,但它看起来很复杂,没有任何解释..

有没有人有这方面的想法?谢谢!

1 个答案:

答案 0 :(得分:2)

Slick希望在“个人资料”投影中将用户名设为选项[字符串]

 case class UserProfile (
  userName: String, // Here
  firstName: Option[String],
  lastName: Option[String],
  fullName: Option[String],
  email: String,
  avatarURL: Option[String])

显然,这里的Slick不知道如何使用case类映射列。