Problem:
node_load and node_save causes an an error.

Here the Error Message
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'seven-nodeblock-1061' for key 'tmd': INSERT INTO {block} (module, delta, theme, status, region, pages) 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); Array ( [:db_insert_placeholder_0] => nodeblock [:db_insert_placeholder_1] => 1061 [:db_insert_placeholder_2] => seven [:db_insert_placeholder_3] => 0 [:db_insert_placeholder_4] => -1 [:db_insert_placeholder_5] => ) in drupal_write_record() (Zeile 7170 von
/includes/common.inc).

Hint:
I commented out the line which sets the format of the body field to exclude that this line causes the error.

Here my code i want to execute:

$nodes = db_query("select nid from {node}");
  foreach ($nodes as $node) {
    echo $node->nid . " / ";
    $node = node_load($node->nid);
    if (isset($node->body)) {
      foreach ($node->body as $bodyLanguageKey => $bodyLanguage) {
        foreach ($bodyLanguage as $bodyKey => $body) {
          if (isset($body["format"])) {
            //$node->body[$bodyLanguageKey][$bodyKey]["format"] = 2;
            echo "BODY FORMAT SET /";
          }
          else {
            echo "FAILUE: NO BODY FORMAT SET /";
          }
        }
      }
      node_save($node);
    } else {
      echo "FAILUE: NO BODY SET /";
    }
    echo "<hr>";
  }

Comments

Johnny vd Laar’s picture

Issue summary: View changes
Johnny vd Laar’s picture

Status: Active » Postponed (maintainer needs more info)

So if I understand it correctly then your node already exists so you are doing a node save. NB only does an insert on the blocks table during node save when this query:
$nid = db_select('nodeblock', 'nb')
->condition('nid', $node->nid)
->fields('nb', array('nid'))
->execute()->fetchColumn();

does not return a nid (can you verify this for me).

When this is the case then it goes into the node_insert function to ensure that an entry in the blocks table is created.

In your situation nodeblock table does not have an entry and blocks table does have an entry. I have added the nodeblock table a number of months ago. So it might be the case that your site is an upgrade from an older version of the module?

I think doing a rehash of your blocks should fix everything (_block_rehash). It's also sufficient to visit the blocks page.

rpsu’s picture

I had similar issue with 'duplicate entry' SQL error after I had export/imported a node from live site to dev environment. Error appeared when I tried to save this imported node.

Deleting node and re-creating it with copy-pasted content solved the issue.

I suppose node import/export does not clone nodeblock's block settings and that leads to this error.

Johnny vd Laar’s picture

I tried node export and import and I cannot reproduce this behavior. Perhaps can you post the code of the node that you are trying to import?

ps. sorry for the late reaction.

linkanp’s picture

I had this issue in one of my site and the Node was a large webform. So deleting this Node and re-create was very time consuming. So I have tried a alternative way. I found the all entry from the 'block' table by my Node ID and then delete those entry from database. It resolves my issue.
select * from block where delta = 'your_nid'
Then delete all returning entry from database.
Thank You.

legolasbo’s picture

linkanp,

Were any messages logged? Can you post them here if there were? perhaps they can point us in the right direction to solve this issue.

linkanp’s picture

No message logged other than the above 'PDOException: SQLSTATE[23000]: Integrity constraint violation:' error message. It was just a assumption that if I delete existing entries for a node then it might resolve the issue and it worked. That's it. Thank You.

Johnny vd Laar’s picture

Status: Postponed (maintainer needs more info) » Fixed

I have rewritten the node_load/insert/update. Can you please verify if this is fixed now?

Status: Fixed » Closed (fixed)

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

4rustynails’s picture

Version: 7.x-1.4 » 7.x-1.6

We have a content type called a reusable block. This content type allows us to place content that might be suitable for a sidebar on any page where the reusable block as a nodeblock is selected from within the options of a main content page. We have been using nodeblock 7.x-1.2 with good success in combination with the reusable block content type.

We recently updated to nodeblock 7.x-1.6 which has seemingly caused all of the reusable blocks to fail to be displayed on the pages where they are assigned. If we attempt to edit one of these reusable blocks, we get the following error which seems to be the same as the bug fixed for version 7.x-1.4.

Error:
4   18/Aug 11:04  error     node     PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'cbp_gov_theme-nodeblock-91002' for key 'tmd': INSERT INTO {block} (module, delta, theme, status, region
 5   18/Aug 11:04  error     php      PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'cbp_gov_theme-nodeblock-91002' for key 'tmd': INSERT INTO {block} (module, delta, theme, status, region

Please let me know what additional information I can provide to help troubleshoot this problem.