尝试插入重复记录后,如果不存在则插入失败

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了尝试插入重复记录后,如果不存在则插入失败相关的知识,希望对你有一定的参考价值。

我使用过this question的解决方案。

我用了接受的答案:

方法

def insertRequiredDocIfNotExists(keys: DocumentInfo) = {
    val documentMasterId = "hardcoded_value 1"
    val documentMasterCategory = "hardcoded value 2"

    val action = {
      RequiredDocuments.filter(_.required_document_id__k === keys.documentId)
        .result
        .headOption
        .flatMap{
          case Some(doc) =>
            logger.debug("Required doc already exists")
            DBIO.successful(doc)
          case None =>
            RequiredDocuments.map(rd => (
              rd.doc_master_list__k,
              rd.doc_category__k,
              rd.applicant_id__k,
              rd.application_id__k,
              rd.name,
              rd.required_document_id__k,
              rd.type_k
            )) += (
              Some("Job Application 1"),
              Some("Resume"),
              Some("Job Applicant 1"),
              Some("Lead for Applicant 1"),
              Some("Resume ID 224"),
              Some("Last 6 months WorkExperience Resume"),
              Some("General")
            )
        }
      }.transactionally

      val db = Connections.getSaleForceSchemaDBObject().db
      db.run(action)
    }

用法

Await.result(insertRequiredDocIfNotExists(keys), Duration.Inf)
// inserts into other tables.

日志

错误消息如下所示:

ERROR: duplicate key value violates unique constraint "hcu_idx_required_document__c_required_document_uuid__c"
Detail: Key (required_document_id__k)=(c63386c0-d599-4d47-a535-941a24a07a6e) already exists.: org.postgresql.util.PSQLException
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "required_document_id__k"
Detail: Key (required_document_uuid__c)=(c63386c0-d599-4d47-a535-941a24a07a6e) already exists.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:132)
at slick.jdbc.LoggingPreparedStatement.$anonfun$executeUpdate$5(LoggingStatement.scala:155)
at scala.runtime.java8.JFunction0$mcI$sp.apply(JFunction0$mcI$sp.java:12)
at slick.jdbc.LoggingStatement.logged(LoggingStatement.scala:84)
at slick.jdbc.LoggingPreparedStatement.executeUpdate(LoggingStatement.scala:155)
at slick.jdbc.JdbcActionComponent$InsertActionComposerImpl$SingleInsertAction.$anonfun$run$11(JdbcActionComponent.scala:510)
at slick.jdbc.JdbcBackend$SessionDef.withPreparedStatement(JdbcBackend.scala:386)
at slick.jdbc.JdbcBackend$SessionDef.withPreparedStatement$(JdbcBackend.scala:381)
at slick.jdbc.JdbcBackend$BaseSession.withPreparedStatement(JdbcBackend.scala:448)
at slick.jdbc.JdbcActionComponent$InsertActionComposerImpl.preparedInsert(JdbcActionComponent.scala:501)
at slick.jdbc.JdbcActionComponent$InsertActionComposerImpl$SingleInsertAction.run(JdbcActionComponent.scala:507)
at slick.jdbc.JdbcActionComponent$SimpleJdbcProfileAction.run(JdbcActionComponent.scala:30)
at slick.jdbc.JdbcActionComponent$SimpleJdbcProfileAction.run(JdbcActionComponent.scala:27)
at slick.basic.BasicBackend$DatabaseDef$$anon$2.liftedTree1$1(BasicBackend.scala:275)
at slick.basic.BasicBackend$DatabaseDef$$anon$2.run(BasicBackend.scala:275)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

Slick有一个upsert方法,可以更新项目(如果存在)。我不想使用upsert,因为每个周期最多不断更新同一项目20次是没有意义的。

答案

好的,这个问题的一个可能的解决方案是使两端的事务查询您要检查的存在值然后可能插入...考虑到DBIOAction是一个monad,您可以编写您的解决方案,如:

val query = RequiredDocuments.filter(_.required_document_id__k === keys.documentId)

val newRecord = {
  (
    Some("Job Application 1"),
    Some("Resume"),
    Some("Job Applicant 1"),
    Some("Lead for Applicant 1"),
    Some("Resume ID 224"),
    Some("Last 6 months WorkExperience Resume"),
    Some("General")
  )
}

val finalQuery = {
  query.exists.result.flatMap{ exitingRecord =>
    if (!exitingRecord) {
      RequiredDocuments.map{rd => (
        rd.doc_master_list__k,
        rd.doc_category__k,
        rd.applicant_id__k,
        rd.application_id__k,
        rd.name,
        rd.required_document_id__k,
        rd.type_k
      )} += newRecord
    } else {
      DBIO.successful(None)
    }
  }
}
db.run(finalQuery.transactionally)

希望有帮助:)

以上是关于尝试插入重复记录后,如果不存在则插入失败的主要内容,如果未能解决你的问题,请参考以下文章

如果不存在则插入记录的MySQL条件[重复]

MySQL INSERT插入条件判断:如果不存在则插入

MySQL:数据存在则更新,不存在则插入

如果行中不存在所有值,则插入记录[重复]

CodeIgniter - 如果新的或更新的重复记录则插入活动记录

如果不存在Mysql,则插入[重复]