Hi , I'm running Drupal 6.14
the following error appear during updating and inserting

Duplicate entry '14-0' for key 1 query: INSERT INTO image_attach (nid, iid, weight) VALUES (14, 0, 2) in ....sites/all/modules/image/contrib/image_attach/image_attach.module on line 349.

The result in Db is
nid iid weight
14 0 1
14 19 0

Comments

joachim’s picture

Status: Active » Postponed (maintainer needs more info)

Were you just saving a node? Or were you doing something else?

This is an error that crops up on updating the module: #207557: update 5-1 to 5-2 gives user warning: Duplicate entry '611-thumbnail' for key 1 query in image_update_5200 but I'd be interested to hear about other things that can (more reliably!) produce it.

iko’s picture

I got this error message with the same circumstances ; it seems to occur when the "attach existing images" option is activated. The "line with the problem" in image_attach/image_attach.module is 281 when attaching the image and 358 when saving/updating the node (the node the image is attached to).

There is no problem when "attach existing images" is not activated.

joachim’s picture

You definitely have a 0 for a iid?? That's not normal at all!

iko’s picture

The first image created through image_attach had an iid = 0, yes. Then the number was correct.

recrit’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new3.52 KB

this issue occurs because the iid = 0 ('None') gets saved to the node regardless of image_attach_node_form_submit() removing unset($form_state['values']['iids'][0]). Then image_attach_nodeapi operates on $node->iids which has the iid=0 saved in it.

Fix:
Validate iid and nid before inserting into db table. INSERT happens in more than 1 place, so I have created a wrapper function (_image_attach_write_record) to insert into the table and utilize drupal_write_record. This makes a common handler for inserting per the defined schema, ie create the record and check if the primary key already exists or not. Since this is now a common function, we can pull the weight incrementing into it to simplify some of the surrounding code when going to insert... so now you just have to call _image_attach_write_record($nid, $iid) .

Status: Needs review » Needs work

The last submitted patch, image_attach-607444.patch, failed testing.

recrit’s picture

Status: Needs work » Needs review
StatusFileSize
new3.56 KB

oops.. original patch was in the image_attach directory.
Here's a re-roll from the image directory.

Status: Needs review » Needs work

The last submitted patch, image_attach-607444-1.patch, failed testing.

joachim’s picture

Status: Needs work » Postponed (maintainer needs more info)

Could you compare with the patch over at #747900: warning: array_filter() The first argument should be an array ... image_attach.module on line 349 please?

> this issue occurs because the iid = 0 ('None') gets saved to the node regardless of image_attach_node_form_submit() removing unset($form_state['values']['iids'][0]). Then image_attach_nodeapi operates on $node->iids which has the iid=0 saved in it.

I'm not managing to reproduce this. Can you give your exact steps please?

recrit’s picture

747900 seems to a address a different issue. My submitted patch addresses the original issue above that occurs when the option 'None' is selected as well as 1 existing image is selected. It will reproduce the error as stated above:

Duplicate entry '14-0' for key 1 query: INSERT INTO image_attach (nid, iid, weight) VALUES (14, 0, 2) in ....sites/all/modules/image/contrib/image_attach/image_attach.module on line 349.

An image_attached gets saved for iid = 0 and then it attempts to insert again.

I am not sure why the patch has failed, it is off of the latest dev.

joachim’s picture

StatusFileSize
new46.28 KB

> when the option 'None' is selected as well as 1 existing image is selected.

Like this?

607444-image_attach_iids-1.png

I'm still not able to reproduce your error.

recrit’s picture

yes, just like that... only difference is that I am using the BetterSelect module.
I just uninstalled betterselect and tested with original image_attach code and everything works fine. I can't quite pinpoint where the betterselect module causes this conflict, but it does.

joachim’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

This is not the first time I've seen BetterSelect break things. File a bug report on BetterSelect :)

recrit’s picture

Title: duplicate entry » duplicate entry error in Image Module
Project: Image » Better Select
Component: image_attach » Code
Status: Closed (won't fix) » Active

updated title and moved to Better Select issue queue as active

andrebonfanti’s picture

subscribe