尽管检查了Integrity约束违规错误

时间:2017-10-26 10:39:19

标签: php laravel

我正在使用laravel 5.2,我正在检查在更新或创建记录之后是否存在3个ID的组合。   我在first_id, second_id, third_id field上有唯一的密钥索引。不过,偶尔我会得到:Integrity constraint violation: 1062 Duplicate entry '103797-5371-D8BTmX0GAi' for key 'first_id'.

  public function notification($fId, $sId, $tId)
  {
    $getEmail = Input::get('email');
    $email = new Email($getEmail);

    if(!$email->isValid()){
      return Response::json(['error_message' => self::ERROR_MESSAGE_INVALID_EMAIL], 422);
    }

    $searchEmail = DB::table('newsletter')
      ->select('id', 'status', 'email')
      ->where(['first_id' => $fId, 'second_id' => $sId, 'third_id' => $tId])
      ->first();

    if($searchEmail){
      $updateEmail = DB::table('newsletter')
        ->where(['first_id' => $fId, 'second_id' => $sId, 'third_id' => $tId])
        ->update(['email' => $getEmail, 'deleted_at' => NULL, 'status' => 1, 'updated_at' => Carbon::now()]);
      if($updateEmail){
        return Response::json('Success',201);
      }
    }else{
      $result = DB::table('newsletter')
        ->insertGetId(['email' => $getEmail, 'first_id' => $fId, 'second_id' => $sId, 'third_id' => $tId]);
      if($result){
        return Response::json('Success',201);
      }
    }
  } 

我们拥有巨大的流量,我们每周会收到5-6次这样的错误。尽管我们有记录检查存在与否。   这可能是DB同步问题吗?或者代码有问题吗?

0 个答案:

没有答案
相关问题