When I create a blog post with three terms in the freetagging section, here's the mysql warning message I get:

* user warning: Duplicate entry '1-101' for key 1 query: INSERT INTO term_node (nid, tid) VALUES (101, 1) in /home/rockth5/public_html/stage/includes/database.mysql.inc on line 167.
* user warning: Duplicate entry '1-101' for key 1 query: INSERT INTO term_node (nid, tid) VALUES (101, 1) in /home/rockth5/public_html/stage/includes/database.mysql.inc on line 167.
* user warning: Duplicate entry '1-101' for key 1 query: INSERT INTO term_node (nid, tid) VALUES (101, 1) in /home/rockth5/public_html/stage/includes/database.mysql.inc on line 167.

Notice how each of these is trying to associate node 101 with term 1. It should be associating 101 with 37, 45, and 82 (examples). For it to be associating each of those with 1 means something's broken in the taxonomy wrapper.

This in turn prevents category from cooperating with Views and other taxonomy dependant modules.

Hope this helps!

Comments

baal32’s picture

I'm having the same problem with the tid not being passed correctly to term_node

smoothify’s picture

I posted a possible solution on a different issue (regarding tagadelic), but the problem was the same.

Basically the insert routine for term_data wasn't keeping track of the auto increment id for the tag and so when it went to insert into term_node it would use an invalid one.

see http://drupal.org/node/119938 or below:

i've been looking at this today and think i found the problem with the free tagging.

Its in the taxonomy_save_term function, around line 337.

I added the following lines above the Insert routine:

    if (variable_get('taxonomy_maintain_db', 1)) {
       // Fix for free tagging to be recorded in legacy database
       $edit['tid'] = db_next_id('{term_data}_tid');
       $curr_tid =  $edit['tid'];
       // End of Fix
       db_query("INSERT INTO {term_data} (tid, name, description, vid, weight) VALUES (%d, '%s', '%s', %d, %d)", $edit['tid'],   $edit['name'], $edit['description'], $edit['vid'], $edit['weight']);

Before the $edit['tid'] wasn't being set at all resulting in insert errors, or incorrect id's.

Sorry i don't have time for a patch, but this is now working for me.

venkat-rk’s picture

I just wonder if this bug is at the heart of all those duplicate entry errors that have been alive in many category related modules (and perhaps still are) ever since category was released. I read somewhere else on the forum in connection with some other module(s) that duplicate entry errors happen when the auto incrementing fails. Sorry I am unable to dig up the links.

Just wanted to share this observation.

smoothify’s picture

I have just run into some problems with my fix - the term_node and term_data table seem to occasionally get out of sync,

I think this is definitely the right area but we need to make sure its synced better with the category tables and also possibly put a lock on to prevent a race condition.

I will keep looking into this if i get a chance.

smoothify’s picture

I spent some time on this yesterday but didn't really get anywhere. I gave up in the end and uninstalled the module and returned to the basic drupal taxonomy module.

What i did find out though is when you insert / update a node using free tagging (and there are new categories to add) then the legacy routines in the wrapper get fired twice.

Once before the actual category node is created and once after. The problem here is the routine before the category node is added isn't aware of the id of the next category to be added, and the routine after isn't aware of the id of the node.

I think the taxonomy wrapper really needs an overhaul, to be simplified and most of the logic moved into the actual category module, as its so hard to work out exactly whats happening and in what order.

Will be following the progress though as i'd like to use category in my upcoming sites.

Ben

inforeto’s picture

mariagwyn’s picture

subscribing. I am having a similar problem with the category module, freetags and updates.

lenart’s picture

Version: 5.x-1.1 » 5.x-1.x-dev

Same problem here. I have category.module (5.x-1.x-dev from 2007-Mar-06), free-tagging and multiple select on containers (which is anyway true for free-tagging).

- If I choose existing terms it works fine
- If I remove or add other existing terms from the node it works fine
- If I add one non-existing term it works fine
- If I want to add two (or more) new terms it breaks

user warning: Duplicate entry '0-68' for key 1 query: INSERT INTO term_node (nid, tid) VALUES (68, 0) in /includes/database.mysql.inc on line 172.

lenart’s picture

After some more debugging i've discovered this:
The problem lies in line 550 of category's taxonomy.module
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $typed_term_tid);
Obviously the variable $typed_term_tid is blank all the time when trying to insert the terms in the table.

Here's the output when I tried to freetag a node with three non-existing terms (captured before the INSERT INTO {term_node} statement):

$ttid:
$typed_term: myterm1
$typed_input: Array ( [60] => myterm1, myterm2, myterm3 )

$ttid:
$typed_term: myterm2
$typed_input: Array ( [60] => myterm1, myterm2, myterm3 )

$ttid:
$typed_term: myterm3
$typed_input: Array ( [60] => myterm1, myterm2, myterm3 )

I'm digging deeper now and I hope to find the problem. I assume it is in the loops around line 536.

P.S. I've also noticed that the table term_data is not cleaned up after deleting all nodes and their related categories.

pacesie’s picture

The problem maybe clear already, just to leave another case of confirmation.

When I use one container with free tagging and one without, choosing one category in each when creating content results in:

user warning: Duplicate entry '1-42' for key 1 query: INSERT INTO term_node (nid, tid) VALUES (42, 1) in /home/serusien/drupal-5.1/includes/database.mysql.inc on line 172.

It happens no matter whether the free tag is newly created or not. I am using drupal 5.1.

pacesie’s picture

Title: Category not creating proper entries in term-node table. » Future database problem?

Will this error leave some sort of problem in the database even I deleted all contents and categories that created this error?

inforeto’s picture

Title: Future database problem? » Category not creating proper entries in term-node table.

The main effect is that nodes aren't associated with their proper taxonomies.
For corrections, editing of nodes and categories might be safer than manually working with the database.

Check the available threads on this subject.
http://drupal.org/node/87669
http://drupal.org/node/59163
etc.

mgerra’s picture

Found same problem on my 5.1 site yesterday. I'm using following:
Taxonomy - 1.330.2.2 2007/01/25
Category - 1.124.2.2 2007/02/02

Interestingly, I get the same errors:
user warning: Duplicate entry '1-280' for key 1 query: INSERT INTO term_node (nid, tid) VALUES (280, 1)

But, am not using free-tagging. Rather, I'm just trying to insert 2 different categories from 2 separate containers (vocabs) on my node form. Error goes away when I select only one category to add, regardless of which container it comes from.

Keen to help and find a solution.

schildi’s picture

Has somebody checked the patch provided by richardfullmer on http://drupal.org/node/121399 ?

sjh-1’s picture

If the patch above works, then this would be a duplicate of http://drupal.org/node/87669 I've submitted a slightly different patch there and some investigation into where the bug was introduced.

bdragon’s picture

Status: Active » Closed (duplicate)

Marking as duplicate of http://drupal.org/node/87669.

Thank you for your report.
--Brandon