Hi, when I generate redirects sometimes get an error message:

An AJAX HTTP error occurred. HTTP Result Code: 500 Debugging information follows. Path: /batch?id=111&op=do StatusText: Service unavailable (with message) ResponseText: PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '5Unw3RhlsfSgQIv1tvrY2ajsov3Dsm9KswXYQ-0XNMg' for key 'hash': INSERT INTO {redirect} (hash, type, uid, source, source_options, redirect, redirect_options, language, status_code, count, access) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10); Array ( [:db_insert_placeholder_0] => 5Unw3RhlsfSgQIv1tvrY2ajsov3Dsm9KswXYQ-0XNMg [:db_insert_placeholder_1] => redirect [:db_insert_placeholder_2] => 1 [:db_insert_placeholder_3] => clothu [:db_insert_placeholder_4] => a:0:{} [:db_insert_placeholder_5] => node/117 [:db_insert_placeholder_6] => a:0:{} [:db_insert_placeholder_7] => und [:db_insert_placeholder_8] => 0 [:db_insert_placeholder_9] => 0 [:db_insert_placeholder_10] => 0 ) in drupal_write_record() (line 7136 of /home/quickstart/websites/drupal.dev/includes/common.inc).

Comments

zestagio’s picture

Issue summary: View changes
zestagio’s picture

StatusFileSize
new8.88 KB
new54.4 KB
zestagio’s picture

pere orga’s picture

Status: Active » Postponed (maintainer needs more info)

Hmm, I understand that this patch fixes your problem but, how can this occur?

Could you give us some steps of how to reproduce the problem you are experiencing? I've never seen it.

zestagio’s picture

This error occurs very rarely. What would you reproduce it with a high probability, it is necessary to generate a large number of nodes (> 500) with a module devel

zestagio’s picture

Also for the greater likelihood of reproducing the bug, you can generate a large number of redirects using devel

pere orga’s picture

Status: Postponed (maintainer needs more info) » Active

ok

pere orga’s picture

Status: Active » Needs review
mrmikedewolf’s picture

StatusFileSize
new594 bytes

Good patch. I've just renamed it to follow drupal.org conventions. Please credit original author for the content.

mrmikedewolf’s picture

Actually, I double back. I needed to make an additional modification to avoid the error. With the existing patch, if a duplicate hash exists, the query is switched from an insert query to an update query, which is correct, but I still get the Integrity constraint because when it saves, it does so with a new rid. What actually needs to happen is if there is an existing redirect that matches the new one, it should be updated. So the rid of the existing redirect needs to be added to the new one.

mrmikedewolf’s picture

Here is my patch.

jami’s picture

Status: Needs review » Reviewed & tested by the community

I was running into this problem at around row 400-500 of 1800 nodes I was adding redirects to. With the patch in 11 applied, it works perfectly. Thank you!

My code, in case it helps explain why this happens:

function ltog_lessons_update_7173() {
  $query = new EntityFieldQuery();

  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'lesson');
//  $query->range(0,100);
  $result = $query->execute();

  if (isset($result['node'])) {
    $lesson_nids = array_keys($result['node']);
    $lessons = entity_load('node', $lesson_nids);
  }

  foreach ($lessons as $lesson) {
    $loid = explode("-", $lesson->field_loid['und'][0]['value']);
    $unit_id = $loid[0];
    $redirect_path = "lessons/unit" . $unit_id . "/lesson".  $loid[1] . ".html";

    if ($loid[1] > 0) {
      $redirect = new stdClass();

      module_invoke(
        'redirect',
        'object_prepare',
        $redirect,
        array(
          'source' => $redirect_path,
          'source_options' => array(),
          'redirect' => 'node/' . $lesson->nid,
          'redirect_options' => array(),
          'language' => LANGUAGE_NONE,
        )
      );

      module_invoke('redirect', 'save', $redirect);
    }
  }
}
rp7’s picture

#11 fixed this for me. Thanks!

rkeppner’s picture

#11 also worked for me.

kevinwal’s picture

Hi rkeppner! :)

I have the same in D8. Any insight to where that is in the D8 version?

pifagor’s picture

pifagor’s picture

RTBC

davemybes’s picture

pifagor’s picture

  • pifagor committed bfa0385 on 7.x-2.x authored by mrmikedewolf
    Issue #2260499 by mrmikedewolf, zestagio, pifagor, Pere Orga, jami, rp7...

  • pifagor committed ad9c1a5 on 7.x-1.x
    Issue #2260499 by mrmikedewolf, zestagio, pifagor, Pere Orga, jami, rp7...
pifagor’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

jummonk’s picture

I encounter the same issue. It happens actually do an update of a node with multiple redirects with the same source and language. The redirect_hash module can give the hash of the other redirect which leads to an update with the correct rid but the hash from another redirect => integritey constraint violation. This should be solved by redirect_hash taking the rid also into account to calculate the hash. What I still don't get is how the initial situation is possible. (The multiple redirects with same source and language and different hashes.)