When updating an FAQ node, and only changing the answer, the following user warning is issued:

user warning: Duplicate entry '78-78' for key 'PRIMARY' query: INSERT INTO faq_questions (nid, vid, question, detailed_question) VALUES(78, 78, 'question title here', '') in /path/to/webroot/sites/all/modules/faq/faq.module on line 246.

this is caused by the following piece of code (faq.module on line 246)

  else {
    db_query("UPDATE {faq_questions} SET question = '%s', detailed_question = '%s' WHERE nid = %d AND vid = %d", $node->title, $node->detailed_question, $node->nid, $node->vid);
    if (!db_affected_rows()) {
      faq_insert($node);
    }
  }

The problem is that db_affected_rows() returns 0 if no data is changed, which is the case when the question and detailed_question remain the same. It will then do another faq_insert...
My question is... why? It is in the node_update function, so the node should already exist right?

Suggested resolutions:

  • remove the check and insert
  • change the check

What do you think is the best way to go?

Comments

jeremy.zerr’s picture

I am getting the same exact error on my setup. Running 6.x-1.11.

I totally agree about the fact that the node should already exist at this point, so there should be no need to do an insert.

Looking through the CVS repository, the change came in version 1.1.4.52.2.87 in the DRUPAL-6--1 branch, and the comments link to the issue: #328464: Edit does not working after changing type

Here is the link to the diff in CVS, which was only these specific lines:
http://drupalcode.org/viewvc/drupal/contributions/modules/faq/faq.module...

So the reason for that code was to support importing nodes and changing types. So I think its pretty safe to remove that code for most normal usage.

I'm not very familiar with the process of importing nodes, so I don't know if there is a better way. It seems like this bit of code is for a use case that is very uncommon, and breaks a use case that is very common (updating the answer to the question without changing the question itself).

A possibly better solution to handle all cases could be to test first if the nid actually exists in the faq_questions table, if it does, do an update, otherwise do the insert. That adds an extra SQL query which is not ideal. Anybody else have different ideas?

I removed the lines added in the diff shown above and did some very minimal testing and seems to work OK, which to me means the error is gone on Update.

Thanks,
Jeremy Zerr
http://www.zerrtech.com

stella’s picture

Status: Active » Fixed

I've removed the check, thanks!

stella’s picture

Released in 5.x-2.15 and 6.x-1.12.

Status: Fixed » Closed (fixed)

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