Forum OpenACS Q&A: can't delete a record in ec_custom_product_field_values

Hello All,

I just encountered an extremely weird issue with Ecommerce 4.x.

I can add products in two ways - a bulk upload via the upload
utilities and by using the Add product features in Ecommerce.

Someone noticed that they can delete some products while other
products would return a referrential integrity error when attempting
to delete.

After several hours of trying to figure out what was wrong with the
code I looked into the database and started manually executing the
qurries in the delete.tcl script. It so happened that I was trying to
delete a product I added using the add product feature. I tried

"delete from ec_custom_product_field_values where product_id=8523"

It returned

DELETE 0

which seems impossible because I have a record there.

Then I got a little frustrated and tried

"delete from ec_custom_product_fields"

which would have delete everything inside the table
but lo and behold. All the records are still intact when I did a

"select * from ec_custom_product_fields"

But then I started to delete a product which was added via the upload
utilities and it worked.

Has anybody encountered this strange phenomenon before ? I am
thinking this could be a postgres issue - I am using PG7.1 but then
again it could be a trigger or something like that which I am not
seeing.

Thanks,

In the past there were several trigger bugs in ecommerce where the trigger was doing return new instead of return old.  Return new always returns null and cancels the delete.  It might be something else, but try looking for ecommerce delete triggers that return new.

Thanks for the swift response.

Below is the only trigger I found that deals with ec_custom_product_field_values

create function ec_custom_p_f_values_audit_tr ()
returns opaque as '
begin
        insert into ec_custom_p_field_values_audit (
        product_id,
        last_modified,
        last_modifying_user, modified_ip_address
        ) values (
        old.product_id,
        old.last_modified,
        old.last_modifying_user, old.modified_ip_address
        );
	return new;
end;' language 'plpgsql';

create trigger ec_custom_p_f_values_audit_tr
after update or delete on ec_custom_product_field_values
for each row execute procedure ec_custom_p_f_values_audit_tr();

I noticed that the triger is "after update or delete". I'm going to experiment with the above and see if it solves my problem or it might create a new one.

Thanks Again.

You need an if statement that looks something like:

if TG_OP = ''delete'' then
   return old;
else
   return new;
end if;
Hamilton, I'd appreciate it if you could upload a patch to the SDM for this problem when you've sorted it out. This would save others the frustration you went through. I'll make sure it gets applied asap.

/Bart

You can count on it Bart.
Hello Everyone,

I could not find the SDM for the Ecommerce 4.x package. Should I put the patch in the Ecommerce (ACS 3 Based) ?

Anyways here is what fixed it. Thanks to Dan for helping me out on the trigger.

The culprit is in package/ecommerce/sql/postgresql/ecommerce-create.tcl

create function ec_custom_p_f_values_audit_tr ()
returns opaque as '
begin
        insert into ec_custom_p_field_values_audit (
        product_id,
        last_modified,
        last_modifying_user, modified_ip_address
        ) values (
        old.product_id,
        old.last_modified,
        old.last_modifying_user, old.modified_ip_address
        );
		if TG_OP = ''INSERT'' then
			return new;
		else
			return old;
		end if;
end;' language 'plpgsql';

create trigger ec_custom_p_f_values_audit_tr
after update or delete on ec_custom_product_field_values
for each row execute procedure ec_custom_p_f_values_audit_tr();

Thanks Again.

You need to change the 'if' statement so that it looks like what I posted above.  Returning 'old' for updates will cause the value not to change, and the update will fail.
Ooops, Sorry about that ''INSERT'' should be ''UPDATE''.

Thanks again,

Hamilton, the current ecommerce package is still a port from the 3.x version, which the name of the SDM module for ecommerce reflects. I can see how this is confusing.

Please go ahead and file the patch there and I'll apply it.

/Bart