我在Scala中有一个简单的case类,我用一个自动生成的主键(一个序列号)将它Map到DB2表。
Slick显示了生成的SQL,问题似乎是它试图(也)更新主键列,而DB2不允许这样做。
下面是Slick生成的SQL语句:
slick.basic.BasicBackend.action - [update "SOME_THING" set "RECORD_ID" = ?, "STATUS" = ? where "RECORD_ID" = ...]
由于我只是尝试更新STATUS
,这就是我想要的(并且应该可以工作):
slick.basic.BasicBackend.action - [update "SOME_THING" set "STATUS" = ? where "RECORD_ID" = ...]
如何修改代码以使UPDATE正常工作?我可以用DELETE+INSERT替换UPDATE,但有更好的方法吗?
使用Slick 3.3.3和Scala 2.12。
import slick.jdbc.DB2Profile.api._
import slick.lifted.ProvenShape
class SomethingTbl(tag: Tag, schemaName: String) extends Table[SomethingRec](tag, _schemaName = Some(schemaName), _tableName = "SOME_THING") {
def record_id: Rep[Long] = column[Long]("RECORD_ID", O.PrimaryKey, O.AutoInc)
def status: Rep[String] = column[String]("STATUS")
override def * : ProvenShape[SomethingRec] = (record_id, status ) <> (SomethingRec.tupled, SomethingRec.unapply)
}
import akka.actor.ActorSystem
import slick.jdbc
import slick.jdbc.DB2Profile
import slick.sql.FixedSqlAction
import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
class SomethingDAO(schemaName: String)(implicit val system: ActorSystem) {
import slick.jdbc.DB2Profile.api._
lazy val someThings: TableQuery[SomethingTbl] =
TableQuery[SomethingTbl](tag => new SomethingTbl(tag, schemaName))
// Capture the DB-generated sequence number and return it
private lazy val insertSomething: jdbc.DB2Profile.IntoInsertActionComposer[SomethingRec, SomethingRec] =
someThings returning someThings.map(_.record_id) into ((rec, record_id) => rec.copy(record_id = record_id))
private implicit val db: DB2Profile.backend.Database = Database.forConfig("slick-db2")
def update(e: SomethingRec): Int = {
val recordFut = db.run(updateSomething(e))
val record = Await.result(recordFut, Duration.Inf)
record
}
private def updateSomething(e: SomethingRec): FixedSqlAction[Int, NoStream, Effect.Write] = {
someThings
.filter(_.record_id === e.record_id)
.update(e)
// com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-798, SQLSTATE=428C9, SQLERRMC=RECORD_ID, DRIVER=4.19.26
}
}
1条答案
按热度按时间bweufnob1#
修改代码如下所示,它工作