在更新语句

时间:2017-12-06 02:53:29

标签: scala types playframework slick

我在使用光滑MappedColumnType时遇到问题,代码段如下:

private trait UserTable {
  self: HasDatabaseConfigProvider[JdbcProfile] =>

  import driver.api._

  lazy val userTable = TableQuery[User]

  class UserTable(tag: Tag)
    extends Table[User](tag, "user") {

    implicit def mapper = MappedColumnType.base[JsObject, String](
      { scope: JsObject => scope.toString }, { s: String => Json.parse(s).as[JsObject] }
    )

    val id = column[Long]("id", O.PrimaryKey, O.AutoInc)
    val name = column[String]("name")
    val hobby = column[JsObject]("hobby")

    def * = (id, name, hobby) <> (User.tupled, User.unapply)
  }

}

我的User案例类定义如下:

case class User(id: Long, name: String: hobby: JsObject)

我的数据库有相应的insertupdate语句。但是,以下更新声明对我不起作用。

  def updateQuery(id: Long, newUser: User) = {
    userTable.filter(x => x.id === id)
      .map(x => (x.hobby))
      .update(newUser.hobby)

它会抛出一个编译错误:

No matching Shape found.
Slick does not know how to map the given types.
Possible causes: T in Table[T] does not match your * projection. Or you use an unsupported type in a Query (e.g. scala List).

我认为这很简单。有什么我做错了吗?

0 个答案:

没有答案