Slick 3: insertOrUpdate not working

I am using slick with Postgresql 9.6.1, Plya! 2.5 and play-slick 2.0.2.

(I also use slick-pg 0.14.3, but I donโ€™t think it is changing anything here.)

I use insertOrUpdatein a very direct way, but I still get a unique exception.

I have a very simple test using insertOrUpdate: if I run it several times, I always get sql exception:

ERROR: duplicate key value violates unique constraint "ga_client_id_pkey"
  Detail: Key (client_id)=(1885746393.1464005051) already exists

However, my table is defined as client_ida primary key:

def clientId = column[String]("client_id", O.PrimaryKey)

and is defined in sql as follows:

client_id    TEXT NOT NULL UNIQUE PRIMARY KEY

The proven function is simple:

db.run(gaClientIds.insertOrUpdate(gaClientId))

and the controller simply calls these methods and does nothing.

It is strange that starting the methods themselves several times does not lead to an error, but the controller does, although it only calls the method.

insertOrUpdateIs the slick function still not sure, or am I missing something?

+9
1

insertOrUpdate MySQL

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
+5

Source: https://habr.com/ru/post/1015031/


All Articles