Scala / Slick 3.0.1 - 更新多个列

时间:2015-08-11 14:17:45

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

每当我收到给定id的更新请求时,我都会尝试更新DB表中的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的文档指出,为了更新多个列,需要使用map来获取相应的列,然后更新它们。

这里的问题如下 - 更新方法似乎接受了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。真的很感激,如果你能对此有所了解。

干杯, Sathish所在

1 个答案:

答案 0 :(得分:5)

(取代原始答案)似乎隐含必须在查询的范围内,这编译:

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())))