Getting this error this morning and I'm fairly sure it's Feeds that is generating it:

Duplicate entry '11-3531' for key 1 query: INSERT INTO term_node (nid, vid, tid) VALUES (3531, 3531, 11) in /modules/taxonomy/taxonomy.module on line 714.

Also getting:

Download of view-source:http://www.example.com/calendar/export.php?calendar=default&type=rss&spo... failed with code -1003.

I did add a category to Taxonomy:Tags mapping a few days ago to my Feeds importer, but the calendar feed doesn't have any categories.

Comments

alex_b’s picture

Status: Active » Postponed (maintainer needs more info)

Do you map to node id?

jaarong’s picture

No, I'm using the Feed importer that comes out of the box with Feeds, it has these mappings by default:

Title Title

Description Body

Published date Published date

Item URL (link) URL

Item GUID GUID

Bevan’s picture

Title: Duplicate entry error » Duplicate entry for term_node
Version: 6.x-1.0-beta9 » 6.x-1.0-beta10
Category: support » bug
Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new1.99 KB

This bug is caused by the following code in taxonomy_feeds_set_target();

  if ($vocabulary->tags) {
    foreach ($terms as $k => $v) {
      // Make sure there aren't any terms with a comma (=tag delimiter) in it.
      $terms[$k] = preg_replace('/\s*,\s*/', ' ', $v);
    }
    // Simply add a comma separated list to the node for a "tags" vocabulary.
    $terms = array_merge($terms, drupal_explode_tags($node->taxonomy['tags'][$vocabulary->vid]));
    $node->taxonomy['tags'][$vocabulary->vid] = implode(',', $terms);
  }

For new items this works fine. But nodes which already exist and are being updated, have the existing terms as objects in $node->taxonomy.

Because the above code does not check for the presence of the terms, each existing term gets added again and is found twice in $node->taxonomy;

  1. as a term object keyed by term ID
  2. in a comma-separated string in $node->taxonomy['tags']

Having the term found twice in $node->taxonomy causes taxonomy_node_save() to attempt save the same node:term relationship twice, because it has no handling for this scenario.

While Drupal core could handle this better in taxonomy_node_save(), a fix for Drupal 6 or 7 is not likely to be accepted and will probably break other contrib modules. Additionally, the fix in Feeds is very simple;

The attached patch fixes it bug by removing the if ($vocabulary->tags) {} section so that all vocabularies are handled equally, regardless of whether the vocabulary is a tagging vocabulary or not. The patch looks more complicated than it is because all of the old else {} is indented one level less.

Please excuse the SVN-not-CVS patch.

Bevan’s picture

Thinking about this a little more, I believe this patch will cause Feeds to neglect a special case where a tag in the source does not already exist as a Drupal Term. Feeds has an (intentional?) feature by which it will create the term if the vocabulary is a tagging vocabulary, but not create the term if the vocabulary is not a tagging vocabulary. AFICT that feature depends on the use of $node->taxonomy['tags'].

I will test and report back

Bevan’s picture

I confirmed that the patch breaks that feature. I guess the better solution is to expand the if ($vocabulary->tags) {} section to check for existing term objects and unsetting them if they are already in $node->taxonomy['tags']. This is quite messy though. Any better ideas?

Bevan’s picture

This patch reverts the previous patch (#3) and extends the if ($vocabulary->tags) {} section to de-duplicate terms that are in both the $node->taxonomy and $node->taxonomy['tags'] sections.

bradjones1’s picture

I'm running into a similar issue but is there any way to fix this without hacking core? Seems like a pretty major fix to what seems to be a module-specific issue.

Bevan’s picture

@bradjones1,

This patch is for Feeds, not Drupal core. Also it not so much a dirty "hack" as opposed to a clean bug fix.

A patch in this issue will probably get committed to Feeds to fix the bug, so as long as you have a way to manage the patch in your code base and update/reapply it or merge it in when you next update Feeds, then it is a safe and clean "hack". On the other hand, the bug that this patch fixes does not cause any data loss, it only causes errors to be reported, so it's not a big deal if it gets lost in an upgrade.

Please review and test the patch if you are able. You can mark it "RTBC" if you are confident it is ready to be committed.

awlo’s picture

Status: Needs review » Reviewed & tested by the community

The second patch works for me just fine.

@Bevan, when uploading files, please make sure they don't have special characters in them (spaces, a comma and a hash in this case), since now the links to patches are broken. To download or view them you have to encode them properly:
http://drupal.org/files/issues/933758%23comment-3925610%20Duplicate%20En...

cedarm’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.18 KB
new1.39 KB

Patch from #6 works, but shouldn't the de-dup come after the array_merge()? Also, I think we can eliminate a nested loop and get array_diff_key() to do some of the work for us.

Patch a is a direct replacement for #6.
Patch b is rolled after new patch for #925264: Don't allow commas in tag names.

bradjones1’s picture

@Bevan - apologies for my nonsensical reply... looking back at this post now I don't even know what I was talking about re: core. I'll try the patch. Thanks for your work on this.

jvieille’s picture

Patch #10 (a) works for me
Thanks

zazinteractive’s picture

Thanks cedarm. 10a fixed it for me

EDIT: actually it reduced the number of duplicate key records but some are still there

Michsk’s picture

I tried 10B but i sometimes keep getting the error:
Duplicate entry '1103-18758' for key 'PRIMARY' query: INSERT INTO term_node (nid, vid, tid) VALUES (18744, 18758, 1103) in /var/www/vhosts/##.nl/httpdocs/modules/taxonomy/taxonomy.module op regel 713.

I must say tough that after applying the patch i get a lot less of them then i did before.

After applying the patch, it looks like something is going wrong with the vocab. Because my path is [vocab-raw]/etcetc but the token does not recognize the vocab so the path stays [vocab-raw]

hyunkeln’s picture

Apply this patch:
+ foreach ($terms as $k => $v) {
+ $alias = taxonomy_get_term_by_name($terms[$k]);
+ if(isset($alias[0]->name))
+ if($terms[$k]!=$alias[0]->name)
+ $terms[$k]=$alias[0]->name;
+ }

before line:

$terms = array_merge($terms, drupal_explode_tags($node->taxonomy['tags'][$vocabulary->vid]));

In file: feeds/mappers/taxonomy.inc

In order to ensure than the terms are new, the other patches fail when terms with special chars are used: español, méxico, Österreich, etc...

gerlos’s picture

I applied your #10 and #15 patches, but still get this error, both with current and dev version of this module. :-(

Solved disabling tags for node types I'm importing (I was importing events using the ical node processor and parsing tags from description using hashtags module).

achton’s picture

Version: 6.x-1.0-beta10 » 6.x-1.x-dev
StatusFileSize
new1.44 KB
new1.55 KB

cedarm's patch 10a in #10 works for me as well.

Here's a re-roll of that patch against 6.x-1.x-dev. Beware that #1567878: Notice: Undefined property: stdClass::$taxonomy affected the logic slightly so this patch won't apply against any current betas. I also modified the comments slightly due to this, so please be aware of that change when you review the patch.

For those who need the patch to apply against current recommended release (beta12), you can use this patch instead (not listed below).

twistor’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)