我正在使用Scala的'Slick和PostgreSQL。 而且我在配單PK的表上工作得很好。 現在我需要使用一個表有多個的PK:斯卡拉與多個PK insertOrUpdate()拋出異常錯誤:輸入結束時的語法錯誤
case class Report(f1: DateTime,
f2: String,
f3: Double)
class Reports(tag: Tag) extends Table[Report](tag, "Reports") {
def f1 = column[DateTime]("f1")
def f2 = column[String]("f2")
def f3 = column[Double]("f3")
def * = (f1, f2, f3) <> (Report.tupled, Report.unapply)
def pk = primaryKey("pk_report", (f1, f2))
}
val reports = TableQuery[Reports]
時,我有空表,並使用reports.insert(report)
它工作得很好。 但是當我使用reports.insertOrUpdate(report)
我接受和例外:
Exception in thread "main" org.postgresql.util.PSQLException: ERROR: syntax error at end of input
Position: 76
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
at ....
我在做什麼錯?如何解決它?
在此先感謝。
PS。我試圖解決辦法 - 試圖通過實施「是否存在更新其他插入」邏輯:
val len = reports.withFilter(_.f1 === report.f1).withFilter(_.f2 === report.f2).length.run.toInt
if(len == 1) {
println("Update: " + report)
reports.update(report)
} else {
println("Insert: " + report)
reports.insert(report)
}
但我仍然得到更新異常:
Exception in thread "main" org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "pk_report"
Detail: Key ("f1", f2)=(2014-01-31 04:00:00, addon_io.aha.connect) already exists.
什麼是'insertOrUpdate',我沒有在'Slick' api中看到這樣的方法。 –
我擁有它,當表有一個PK時,它可以很好地工作。我還能如何實現UPSERT操作? –
Ende Neu:在Slick 2.1-M2中添加insertOrUpdate,並將成爲Slick 2.1的一部分。 – cvogt