光滑3:insertOrUpdate无法正常工作

Sim*_*mon 11 postgresql upsert slick

我正在使用Postgresql 9.6.1,Plya!2.5和play-slick 2.0.2.

(我也使用slick-pg 0.14.3,但我不认为这会改变任何东西.)

我正在insertOrUpdate以非常直接的方式使用,但我仍然得到一个独特的例外.

我有一个非常简单的测试使用insertOrUpdate:如果我运行它几次,我总是得到一个SQL异常:

ERROR: duplicate key value violates unique constraint "ga_client_id_pkey"
  Detail: Key (client_id)=(1885746393.1464005051) already exists
Run Code Online (Sandbox Code Playgroud)

但是,我的表定义client_id为主键:

def clientId = column[String]("client_id", O.PrimaryKey)
Run Code Online (Sandbox Code Playgroud)

并在sql中定义如下:

client_id    TEXT NOT NULL UNIQUE PRIMARY KEY
Run Code Online (Sandbox Code Playgroud)

测试的功能只是:

db.run(gaClientIds.insertOrUpdate(gaClientId))
Run Code Online (Sandbox Code Playgroud)

并且控制器只是调用此方法而不执行任何其他操作.

一个奇怪的事情是,多次启动方法本身不会导致错误,但控制器会执行,尽管它只调用方法.

insertOrUpdate华而不实的功能还不确定还是我失去了一些东西?

Ree*_*uuk 5

仅MySQL驱动程序支持insertOrUpdate

http://slick.lightbend.com/doc/3.2.1/supported-databases.html

您可以尝试使用该库,该库为您实现insertOrUpdate / upsert

https://github.com/tminglei/slick-pg

这就是我们在当前项目中使用它的方式。

import com.github.tminglei.slickpg._
import com.typesafe.config.ConfigFactory
import slick.basic.Capability

object DBComponent {

  private val config = ConfigFactory.load()

  val driver = config.getString("rdbms.properties.driver") match {
    case "org.h2.Driver" => slick.jdbc.H2Profile
    case _               => MyPostgresProfile
  }

  import driver.api._

  val db: Database = Database.forConfig("rdbms.properties")

}

trait MyPostgresProfile extends ExPostgresProfile {

  // Add back `capabilities.insertOrUpdate` to enable native `upsert` support; for postgres 9.5+
  override protected def computeCapabilities: Set[Capability] =
    super.computeCapabilities + slick.jdbc.JdbcCapabilities.insertOrUpdate

  override val api: MyAPI.type = MyAPI

  object MyAPI extends API
}

object MyPostgresProfile extends MyPostgresProfile
Run Code Online (Sandbox Code Playgroud)

  • IMO,该文档让我们认为每个驱动程序都支持它,而我以前从未见过此页面。感谢您的答复。 (4认同)