Scala / Slick 3.0.1 - update multiple columns

Whenever I get an update request for a given identifier, I try to update the masterId and updatedDtTm columns in the DB table (I don't want to update my createdDtTm). Below is my code:

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

The Slick documentation states that to update multiple columns, you must use a map to get the corresponding columns, and then update them.

The problem is this: the update method seems to be Nothing.

I also tried the following, which was done in the same way as described above:

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

When I compile the code, it gives me the following compilation exception:

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

I am using Scala 2.11 with Slick 3.0.1 and IntelliJ as an IDE. I really appreciate if you can shed some light on this.

Cheers, sathish

+4
source share
1 answer

(Replaces the original answer) It seems that the implicit should be in the request area, this compiles:

 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()))) 
+4
source

All Articles