Once i update a product kit i get the following error:

user warning: Duplicate entry '8' for key 'PRIMARY' query: INSERT INTO uc_products (vid, nid, model, list_price, cost, sell_price, weight, weight_units, default_qty, unique_hash, ordering, shippable) VALUES (8, 8, 'asdsddad / IT123123012983 1', 1.4, 0, 2.8, 0, 'lb', 0, '44f7981e0e139c061c789c94093cced8', 0, 0) in /Volumes/SG/httpdocs/drupal/sites/all/modules/ubercart/uc_product_kit/uc_product_kit.module on line 351.

and even though it's a warning the update dot not take place anyway.

btw how came an update is performed doing and INSERT INTO instead of an UPDATE SET..?

CommentFileSizeAuthor
#14 PK_dup_ents-641430.patch2.02 KBgriz
#4 duplicate_entry.png166.86 KBrandallpena
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

TR’s picture

Priority: Critical » Normal
Status: Active » Postponed (maintainer needs more info)

What do you mean by "update a product kit" ? What exactly are you doing when you get that error? Does it happen for all product kits or just one?

Line 351 in uc_product_kit.module is a blank line in Ubercart 6.x-2.2 - are you sure you are using 2.2 and have an unaltered version of the uc_product_kit.module code? That INSERT statement appears on lines 333 and 343. To me it sounds like you have added code or are using an old version, in which case the first thing you should do is make sure you have a clean unmodified version of uc_product_kit then test for the error again.

You ask: "how came an update is performed doing and INSERT INTO instead of an UPDATE SET..?". If you look at the code, you will see an UPDATE is attempted, then if and only if no rows were affected an INSERT is used to add the data to the table.

TR’s picture

Tagging

redamo’s picture

Version: 6.x-2.2 » 6.x-2.4
Component: Code » Products

I too get the following error when trying to edit a product kit. (for instance when promoting on first page.)
user warning: Duplicate entry '167' for key 'PRIMARY' query: INSERT INTO uc_products (vid, nid, model, list_price, cost, sell_price, weight, weight_units, default_qty, unique_hash, ordering, shippable) VALUES (167, 167, 'Any / Any / Any / Any / Any / Any / Any / Any / Any / Any', 0, 0, 99.99, 0, 'lb', 0, '1d6ad7a5ac8612d15785bbcfe92b7c4d', 0, 0) in /usr/home/redamo/drupal-6.19/modules/ubercart/uc_product_kit/uc_product_kit.module on line 313.

randallpena’s picture

Status: Active » Postponed (maintainer needs more info)
FileSize
166.86 KB

I'm also getting this error. Here is how I am able to replicate the error:

1. create a product
a. Name: iPod Nano
b. SKU: AL-123-00001
c. Sell Price: 199.99
d. Attached 5 images
2. create another product
a. Name: iPod Sync Cable
b. SKU: AL-123-00002
c. Sell Price: 0.00
3. create a product kit
a. Name: iPod Nano package
b. As a unit. Customers may only change how many kits they are buying. Do not list component products
c. Total price is blank
d. Products: ipod Nano, iPod Sync Cable
e. After saving, try to update any value of this product kit (e.g. description). You will get the following error message attached.

I am simply following the instructions in chapter 4 of the book "Drupal e-commerce with ubercart 2.x"

Let me know if you need more info. Thanks!

randallpena’s picture

Status: Postponed (maintainer needs more info) » Active
horuskol’s picture

Status: Postponed (maintainer needs more info) » Active

I get this with the 6.x-2.4 release when I'm doing something like changing the image or description of the Product Kit.

db_query("UPDATE {uc_products} SET model = '%s', list_price = %f, cost = %f, sell_price = %f, weight = %f, weight_units = '%s', default_qty = %d, ordering = %d, shippable = %d WHERE vid = %d",
      $obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty, $obj->ordering, $obj->shippable, $obj->vid);
    if (!db_affected_rows()) {
      db_query("INSERT INTO {uc_products} (vid, nid, model, list_price, cost, sell_price, weight, weight_units, default_qty, unique_hash, ordering, shippable) VALUES (%d, %d, '%s', %f, %f, %f, %f, '%s', %d, '%s', %d, %d)",
        $obj->vid, $obj->nid, $obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty,
        md5($obj->vid . $obj->nid . $obj->model . $obj->list_price . $obj->cost . $obj->sell_price . $obj->weight . $obj->weight_units . $obj->default_qty . $obj->ordering . $obj->shippable . time()),
        $obj->ordering, $obj->shippable
      );
    }

As I read this - the problem is that I'm not changing any of those fields in the update query (although, I am changing something about the Product Kit itself) - so db_affected_rows will return 0, causing the insert to be attempted.

Also - since db_affected_rows can also return -1 for error, the insert will NOT be attempted if there was an error in the update query (not sure if this was intentional).

Unfortunately, the only way to get the number of matched rows for an update query through the PHP/MySQL library is to pass a flag when opening the db connection - which would affect all instances of db_affected_rows.

The only thing I can think of is to replace the queries with a single REPLACE - although, that is not a standard SQL query - or we perform a SELECT first to determine if the UPDATE or INSERT is required.

horuskol’s picture

This seems to do the trick:

		$result = db_query("SELECT vid FROM {uc_products} WHERE vid = %d", $node->vid);
		if (db_affected_rows() > 0) {
			db_query("UPDATE {uc_products} SET model = '%s', list_price = %f, cost = %f, sell_price = %f, weight = %f, weight_units = '%s', default_qty = %d, ordering = %d, shippable = %d WHERE vid = %d",
				$obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty, $obj->ordering, $obj->shippable, $obj->vid);
		} else {
			db_query("INSERT INTO {uc_products} (vid, nid, model, list_price, cost, sell_price, weight, weight_units, default_qty, unique_hash, ordering, shippable) VALUES (%d, %d, '%s', %f, %f, %f, %f, '%s', %d, '%s', %d, %d)",
				$obj->vid, $obj->nid, $obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty,
				md5($obj->vid . $obj->nid . $obj->model . $obj->list_price . $obj->cost . $obj->sell_price . $obj->weight . $obj->weight_units . $obj->default_qty . $obj->ordering . $obj->shippable . time()),
				$obj->ordering, $obj->shippable
			);
		}
longwave’s picture

Long SQL statements like that are ugly and hard to maintain; we should switch to drupal_write_record() if at all possible.

tomotomo’s picture

I also see this when calling uc_product_insert($node)

jlporter’s picture

I also have this issue on a new 2.4 install. Only edit an arbitrary attribute and get duplicate key warning, it does seem to save the parts I'm concerned about though.

user warning: Duplicate entry '133' for key 'PRIMARY' query: INSERT INTO uc_products (vid, nid, model, list_price, cost, sell_price, weight, weight_units, default_qty, unique_hash, ordering, shippable) VALUES (133, 133, 'autographed-cd / hatch / bottle-opener', 0, 0, 29.99, 0, 'oz', 0, '1f0a3370cb0a0fc890a42a4e99fccf13', 0, 1) in /mnt/www/retreat.craigcampbell.tv/site/sites/all/modules/ubercart/uc_product_kit/uc_product_kit.module on line 313.

armetrix’s picture

Thanks for your input on this chronic problem. Just where exactly did you place this code? I've attempted to use it a few times and I continue to get the error saying duplicate entry.

horuskol’s picture

/sites/all/modules/ubercart/uc_product_kit/uc_product_kit.module on line 351 < somewhere about there

TR’s picture

Can you post a real patch?

griz’s picture

Status: Active » Needs review
FileSize
2.02 KB

Reviewed and tested by me. Anyone else?

Status: Needs review » Needs work

The last submitted patch, PK_dup_ents-641430.patch, failed testing.

TR’s picture

(Patch looks fine - testbot complains about everything when the issue version is a fixed point release like 6.x-2.4).

griz’s picture

Thanks for the explanation. Git is still something of an enigma to me. What is the next step?

I've altered the dev version for my own site, but I got that through drush. How do you get dev through git?
Alternatively, can I make a diff that will pass testing without using git?

Sorry for the 20 Qs.

TR’s picture

Next step is to wait for someone other than you to verify that the patch works to fix the problem. Hopefully one of the seven people who posted above that they are experiencing the problem can tell us whether your patch fixes the issue. If it does, I'll commit it.

To use git to keep up with the latest version of -dev:

git clone git://git.drupal.org/project/ubercart.git
cd ubercart
git checkout 6.x-2.x

then occasionally
git pull

to get the changes that have been made in the repository and merge them into your private copy.

git status will show you any changes you've made locally that differ from what's in the repository, and git diff will generate a diff file of those differences which can be used as a patch.

That's the basics - lots of help is available on #drupal-gitsupport, feel free to ask even the most elementary git question.

horuskol’s picture

Status: Needs work » Needs review

Hi - sorry for the long gap in responses from me on this issue. I've been distracted by other projects, and then this issue stopped being a problem for me as we implemented a workflow that forces new revisions and so bypassed the bug.

Anyway - I've just tested the patch, and confirm that it resolves the original reported issue when saving products and product kits.

Thanks for your help and effort

longwave’s picture

Suspect that the suggested fix will only work on MySQL due to the way db_affected_rows() works:
http://api.drupal.org/api/drupal/includes--database.pgsql.inc/function/d...

longwave’s picture

Status: Needs review » Needs work
melon’s picture

I just stumbled upon this issue as this is the first time I need product kits.
The patch in #14 works nice for me as well with MySQL.

TR’s picture

Version: 6.x-2.4 » 6.x-2.x-dev
Component: Products » Product kits

Moving to "Product kit" component.

TR’s picture

Status: Needs work » Needs review

Now that the testbot is finally working, let's see if it likes the patch.

TR’s picture

#14: PK_dup_ents-641430.patch queued for re-testing.

longwave’s picture

Status: Needs review » Fixed

The correct pattern for this is just to prefix the insert query with @ according to #805858-3: Affected rows inconsistent across database engines

Fixed in http://drupalcode.org/project/ubercart.git/commitdiff/dfe8b96

Status: Fixed » Closed (fixed)

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