Strange. When trying to update one computed field it gets not updated. Instead the following error is logged for each node:

Duplicate entry '1343' for key 'PRIMARY' query: INSERT INTO content_type_publication SET `field_journal_doi_url_value` = '<a href=\"http://dx.doi.org/10.1016/j.tifs.2011.11.005\" target=\"_blank\" title=\"Digital Object Identifier (DOI)\">10.1016/j.tifs.2011.11.005</a>', `vid` = 1343, `nid` = 511 in /etc/drupal6/all/modules/computed_field_tools/computed_field_tools.module on line 204.

The computed code in question is:

$doi = $node->field_journal_doi[0]['value'];
$url = 'http://dx.doi.org/' . $doi;
$out = '<a href="' . $url . '" target="_blank" title="Digital Object Identifier (DOI)">' . $doi . '</a>';
$node_field[0]['value'] = $out;

Comments

andjules’s picture

Version: 6.x-1.0 » 7.x-1.x-dev

+1 on the 7.x verion
Integrity constraint violation: 1062 Duplicate entry

PatchRanger’s picture

Status: Active » Needs review
StatusFileSize
new1.15 KB

I got the same error recently.

Reason :
We are quering a count of rows that look like our to decide whether we need to insert it or to update.
The bug is in the conditions, they are to strong, we don't ever find anything.
Count is always 0, we are trying to insert - and catching an error about duplicate primary key value.

How to solve :
Delete "Bundle" from query conditions because it IS NOT IN PRIMARY KEY.

Please review the patch.
I have tested it myself - it works.

PatchRanger’s picture

Assigned: Unassigned » PatchRanger
PatchRanger’s picture

StatusFileSize
new2.1 KB

Discovered that we need to delete this too strong condition from db_update also.
Otherwise we got no errors but no updating too.
Tested on self skin. :)
Please review.

roball’s picture

Thanks for the patch, Staratel. Could you please also attach a 6.x compatible patch?

PatchRanger’s picture

We should not port it until we receive feedback about 7.x version.
Also I don't have D6 installed to see what is the problem there and how to port this patch.
Robert, I recommend you to look into these lines :

    db_query("UPDATE {" . $db_table . "} SET `" . $field_db_column_name . "` = " . db_type_placeholder($field_db_column_type) . " WHERE `vid` = %d", $node_field[0]['value'], $result->vid);
    if (db_affected_rows() < 1) {
      // Entry is not yet created, so lets insert.
      db_query("INSERT INTO {" . $db_table . "} SET `" . $field_db_column_name . "` = " . db_type_placeholder($field_db_column_type) . ", `vid` = %d, `nid` = %d", $node_field[0]['value'], $result->vid, $node->nid);
    }

You should debug them to find the problem in 6.x case.
Hope it helps.

wizonesolutions’s picture

Assigned: PatchRanger » Unassigned
Status: Needs review » Needs work

This didn't work for me. I still get:

An AJAX HTTP error occurred. HTTP Result Code: 500 Debugging information follows. Path: /batch?id=15&op=do StatusText: Service unavailable (with message) ResponseText: PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'node-1-0-0-nn' for key 'PRIMARY': INSERT INTO {field_data_field_remaining} (entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_remaining_value) VALUES (:db_insert_placeholder_0_entity_type, :db_insert_placeholder_0_bundle, :db_insert_placeholder_0_deleted, :db_insert_placeholder_0_entity_id, :db_insert_placeholder_0_revision_id, :db_insert_placeholder_0_language, :db_insert_placeholder_0_delta, :db_insert_placeholder_0_field_remaining_value); Array ( [:db_insert_placeholder_0_entity_type] => node [:db_insert_placeholder_0_bundle] => income [:db_insert_placeholder_0_deleted] => 0 [:db_insert_placeholder_0_entity_id] => 1 [:db_insert_placeholder_0_revision_id] => 33 [:db_insert_placeholder_0_language] => nn [:db_insert_placeholder_0_delta] => 0 [:db_insert_placeholder_0_field_remaining_value] => 2950 ) in _computed_field_tools_batch_recompute() (line 513 av /home/wizonesolutions/domains/example.no/public_html/drupal/sites/all/modules/computed_field_tools/computed_field_tools.module).

The weird thing is that it's even trying do an INSERT. The field almost certainly already exists. I'll dig in a bit and see what I can find.

wizonesolutions’s picture

Status: Needs work » Needs review

Actually, this might be a data issue on my own site. I suspect it's the same for others. In any case, setting back to needs review.

wizonesolutions’s picture

OK, it's actually this one: #1587676: Not setting language field

frakke’s picture

Assigned: Unassigned » frakke
Status: Needs review » Closed (fixed)

It should be fixed with the latest build in the repository, HEAD at 7.x-1.x. The patch didn't fix anything.

roball’s picture

Version: 7.x-1.x-dev » 6.x-1.0
Status: Closed (fixed) » Patch (to be ported)

Thanks for fixing it for D7. Could you please also commit to 6.x-1.x?

frakke’s picture

Sure

frakke’s picture

Status: Patch (to be ported) » Closed (fixed)

The 6.x-1.1 version of the module should be available shortly for download. It addresses this issue and adds a few other features/fixes.

roball’s picture

Thanks frakke, v6.x-1.1 seems to work fine.

organicwire’s picture

Version: 6.x-1.0 » 7.x-1.x-dev
Issue summary: View changes
Status: Closed (fixed) » Active

For me there's still an error when recalculating. After a fresh module install, recalculation fails with the following error:

Ein AJAX-HTTP-Fehler ist aufgetreten. HTTP-Rückgabe-Code: 500 Im Folgenden finden Sie Debugging-Informationen. Pfad: /batch?render=overlay&id=31&op=do Statustext: Service unavailable (with message) Antworttext: 
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'node-82-0-0-und' for key 'PRIMARY': INSERT INTO {field_data_field_hotness} (entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_hotness_value) VALUES (:db_insert_placeholder_0_entity_type, :db_insert_placeholder_0_bundle, :db_insert_placeholder_0_deleted, :db_insert_placeholder_0_entity_id, :db_insert_placeholder_0_revision_id, :db_insert_placeholder_0_language, :db_insert_placeholder_0_delta, :db_insert_placeholder_0_field_hotness_value); Array ( [:db_insert_placeholder_0_entity_type] => node [:db_insert_placeholder_0_bundle] => proposal [:db_insert_placeholder_0_deleted] => 0 [:db_insert_placeholder_0_entity_id] => 82 [:db_insert_placeholder_0_revision_id] => 82 [:db_insert_placeholder_0_language] => und [:db_insert_placeholder_0_delta] => 0 [:db_insert_placeholder_0_field_hotness_value] => 0 ) in _computed_field_tools_batch_recompute() (Zeile 535 von $DRUPAL_HOME/sites/all/modules/contrib/computed_field_tools/computed_field_tools.module).

I'm using the latest dev snapshot for D7.

organicwire’s picture

Did patch in #4 really go into the code? It doesn't look like to me.

However, as the error occurred in my drupal installation too, it was caused by another thing. When writing back the computed value into the db, we test if the field will be updated or inserted. Here a wrong compare operator is used:

// Query the database if a computed value already exists
$existing_content = db_select($table_name_current, $table_name_current, ...);

// This causes the error, simply because computed values can contain zero values.
if (empty($existing_content)) {
  // use sql-insert -> error
}

Thus, having a zero value in an existing field, we go for inserting because empty(0) returns true. Instead we should write

if ($existing_content === FALSE) {
  // use sql-insert
}

My patch fixes this.

kmajzlik’s picture

I can confirm bug/error in 7.x-1.0
I can confirm that patch attached in #15 works like a charm - please commit it ASAP and create 7.x-1.1 version.

kmajzlik’s picture

Version: 7.x-1.x-dev » 7.x-1.0
Issue tags: +RTBC
roball’s picture

karlos007, you mean the patch attached to comment #16 fixes this issue? If so, I would suggest to also update this issue's Status accordingly.

kmajzlik’s picture

Status: Active » Reviewed & tested by the community

roball: you are right. my mistake, it was late night here and (i dont know why) i thought that tagging RTBC is enough...

Island Usurper’s picture

While I agree that this patch works, wouldn't it be better to use db_merge()? In the worst case, it's still doing two queries, but it makes the code simpler and easier to read.

GerZah’s picture

Patch #16 works fine for me. Thanks!

roball’s picture

frakke’s picture

This issue should be solved from 7.x-1.1-rc1 and forth.

frakke’s picture

Status: Reviewed & tested by the community » Closed (fixed)