Scala/Slick 3.0.1 - 更新多列

标签 scala intellij-idea scala-2.11 slick-3.0

每当我收到给定 id 的更新请求时,我都会尝试更新数据库表中的 masterId 和 UpdatedDtTm 列(我不想更新我的createdDtTm)。以下是我的代码:

case class Master(id:Option[Long] = None,masterId:String,createdDtTm:Option[java.util.Date],
                      updatedDtTm:Option[java.util.Date])

/**
 * This is my Slick Mapping table
 * with the default projection
 */
`class MappingMaster(tag:Tag) extends
Table[Master](tag,"master") {

    implicit val DateTimeColumnType = MappedColumnType.base[java.util.Date, java.sql.Timestamp](
    {
      ud => new Timestamp(ud.getTime)
    }, {
      sd => new java.util.Date(sd.getTime)
    })
    def id = column[Long]("id",O.PrimaryKey,O.AutoInc)
    def masterId = column[String]("master_id")
    def createdDtTm = column[java.util.Date]("created_dttm")
    def updatedDtTm = column[java.util.Date]("updated_dttm")

    def * = (id.? , masterId , createdDtTm.? , updatedDtTm.?) <>
      ((Master.apply _).tupled , Master.unapply _) }

/**
 * Some where in the DAO update call
 */
db.run(masterRecords.filter(_.id === id).map(rw =>(rw.masterId,rw.updatedDtTm)).
update(("new_master_id",new Date()))

// I also tried the following
db.run(masterRecords.filter(_id === id).map(rw => (rw.masterId,rw.updatedDtTm).shaped[(String,java.util.Date)]).update(("new_master_id",new Date()))

Slick 的文档指出,为了更新多个列,需要使用映射来获取相应的列,然后更新它们。

这里的问题如下 - update 方法似乎接受 Nothing 值。

我还尝试了以下方法,其作用与上面相同:

val t = for {
ms <- masterRecords if (ms.id === "1234")
} yield (ms.masterId , ms.updateDtTm)
db.run(t.update(("new_master_id",new Date())))

当我编译代码时,它给出了以下编译异常:

 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: slick.lifted.FlatShapeLevel
[error]      Source type: (slick.lifted.Rep[String], slick.lifted.Rep[java.util.Date])
[error]    Unpacked type: (String, java.util.Date)
[error]      Packed type: Any
[error]     db.run(masterRecords.filter(_id === id).map(rw => (rw.masterId,rw.updatedDtTm).shaped[(String,java.util.Date)]).update(("new_master_id",new Date()))

我使用 Scala 2.11 和 Slick 3.0.1 以及 IntelliJ 作为 IDE。如果您能对此有所了解,我将不胜感激。

干杯, 萨蒂什

最佳答案

(替换原始答案)似乎隐式必须在查询范围内,这可以编译:

case class Master(id:Option[Long] = None,masterId:String,createdDtTm:Option[java.util.Date],
                    updatedDtTm:Option[java.util.Date])

implicit val DateTimeColumnType = MappedColumnType.base[java.util.Date, java.sql.Timestamp](
  {
    ud => new Timestamp(ud.getTime)
  }, {
    sd => new java.util.Date(sd.getTime)
  })

class MappingMaster(tag:Tag) extends Table[Master](tag,"master") {


    def id = column[Long]("id",O.PrimaryKey,O.AutoInc)  
    def masterId = column[String]("master_id")
    def createdDtTm = column[java.util.Date]("created_dttm")
    def updatedDtTm = column[java.util.Date]("updated_dttm")

    def * = (id.? , masterId , createdDtTm.? , updatedDtTm.?) <> ((Master.apply _).tupled , Master.unapply _)

}

private val masterRecords = TableQuery[MappingMaster]

val id: Long = 123

db.run(masterRecords.filter(_.id === id).map(rw =>(rw.masterId,rw.updatedDtTm)).update("new_master_id",new Date()))

val t = for {
  ms <- masterRecords if (ms.id === id)
} yield (ms.masterId , ms.updatedDtTm)
db.run(t.update(("new_master_id",new Date())))

关于Scala/Slick 3.0.1 - 更新多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31944309/

相关文章:

java - writeConcern 未在 mongodb 中设置为 Acknowledged

java - Scala-Java 互操作,方法重载问题(数组和可变参数)

web-services - Play Framework WS.url 永远卡住

scala - 使用临时凭证从 AWS 外部通过 spark 从 s3 读取

ruby - Rubys 的 each_slice 的 Scala 版本?

java - 如何更新 Java 8 EA (Windows 8)?

java - Gradle 提供了与 Intellij 的依赖关系

java - 使用 Gradle 编译项目时 IntelliJ 到底在做什么?

引用成员类型的 Scala 宏

java - 如何在mongodb中的单个字段中添加值