【问题标题】:In Slick, how to implement multiple projections for a Table?在 Slick 中,如何为一个表实现多个投影?
【发布时间】:2015-04-30 06:42:12
【问题描述】:

这里是代码(我使用的是 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关于这个,但它看起来很复杂,没有解释..

有人对此有想法吗?谢谢!

【问题讨论】:

  • 正如下面@dataBaseServer 提到的,确保您的类型。

标签: scala playframework slick slick-2.0


【解决方案1】:

Slick 期望 Username 在“profile”投影中是 Option[String]

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

显然这里的 Slick 不知道如何将列与案例类映射。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2014-08-01
    相关资源
    最近更新 更多