News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

Cannot remove product if there is FK

Started by MKI-Miro, September 26, 2019, 12:45:51 PM

Previous topic - Next topic

MKI-Miro

Hi

I added FK to db from #__virtuemart_products_sk_sk to #__virtuemart_products

Now I am not able to delete product because of this error:

Cannot delete or update a parent row: a foreign key constraint fails (`puzzleko`.`#__virtuemart_products_sk_sk`, CONSTRAINT `#__virtuemart_products_sk_sk_ibfk_1` FOREIGN KEY (`virtuemart_product_id`) REFERENCES `#__virtuemart_products` (`virtuemart)

Isnt it opssible to to fix code to delete sk_sk entry first and then product?

Thanks

StefanSTS

--
Stefan Schumacher
www.jooglies.com - VirtueMart Invoice Layouts

Please use only stable versions with even numbers for your live shop! Use Alpha versions only if you know what risk you are taking.

MKI-Miro

virtuemart_products_sk_sk has column virtuemart_product_id
i added Foreign Key to this column to reference virtuemart_product.virtuemart_product_id

Now when I want to delete product in administration i get this reference error.

Seems like program tries to remove row in virtuemart_product table (this is not possieble because there is still reference from virtuemart_products_sk_sk)

So I think that program should first delete row in virtuemart_products_sk_sk and then in virtuemart_product table

GJC Web Design

problem is it doesn't...

related lang files are deleted in  \administrator\components\com_virtuemart\helpers\vmtable.php   function delete($oid = null, $where = 0)

maybe u can suggest a change to the dev team
GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

Studio 42

Why do you added Foreign Key ?
This join virtually the 2 tables and it's only here to prevent removing something in one table before the other table.
Remove your Foreign Key and all should work as before

MKI-Miro

1. Need it for my .NET Core App to scaffold DB model correctly
2. I think it makes sens to first delete entity in lang table and then product....

MKI-Miro

is it really so hard to change order of delete?

StefanSTS

If you are a VirtueMart supporter member, you could try to submit it as a bug. Bugs reported by members get higher priority I guess.

Since you are the only one asking for that, and lots of other things need to be done, I guess the priority is in the lower area at the moment.

Regards
Stefan
--
Stefan Schumacher
www.jooglies.com - VirtueMart Invoice Layouts

Please use only stable versions with even numbers for your live shop! Use Alpha versions only if you know what risk you are taking.

AH

Quoteis it really so hard to change order of delete?

You are welcome to make your own changes, this is open source code after all.

It is often not hard to do things once you have spent a great deal of time understanding the complexity
Regards
A

Joomla 3.10.11
php 8.0

MKI-Miro

yes it is open source but once you publish new version, it will override my code...

StefanSTS

Quote from: MKI-Miro on October 16, 2019, 09:23:02 AM
yes it is open source but once you publish new version, it will override my code...

To use your words. Is it really so hard to make the changes, test it properly and propose a core change if it works reliably?

--
Stefan Schumacher
www.jooglies.com - VirtueMart Invoice Layouts

Please use only stable versions with even numbers for your live shop! Use Alpha versions only if you know what risk you are taking.

GJC Web Design

Agree with Stefan, this is what "Open Source" is all about..

You have a request for your particular circumstance .. to my knowledge no one else has ever requested what you want .. so therefore you do the work.. prove it and come back with a core change suggestion.
And "prove it" means testing in every possible case that it is backwards compatible, doesn't affect any other part of the work flows etc etc ..
As u see proving and testing a change is often far more work than the actual coding..
GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

MKI-Miro

So you dont agree that code, which first deletes product and later references to this product, is wrong? really?

MKI-Miro

So what about to change

function delete($oid = null, $where = 0) {

$k = $this->_tbl_key;

if ($oid) {
$this->$k = intval($oid);
}

$mainTableError = $this->checkAndDelete($this->_tbl, $where);

if ($this->_translatable) {

$langs = VmConfig::get('active_languages', array(VmConfig::$jDefLangTag));
if (!$langs) $langs[] = VmConfig::$vmlang;

foreach ($langs as $lang) {
$lang = strtolower(strtr($lang, '-', '_'));
$langError = $this->checkAndDelete($this->_tbl . '_' . $lang);
$mainTableError = min($mainTableError, $langError);
}
}

return $mainTableError;
}


to

function delete($oid = null, $where = 0) {

$k = $this->_tbl_key;

if ($oid) {
$this->$k = intval($oid);
}

if ($this->_translatable) {

$langs = VmConfig::get('active_languages', array(VmConfig::$jDefLangTag));
if (!$langs) $langs[] = VmConfig::$vmlang;

foreach ($langs as $lang) {
$lang = strtolower(strtr($lang, '-', '_'));
$langError = $this->checkAndDelete($this->_tbl . '_' . $lang);
$mainTableError = min($mainTableError, $langError);
}
}

$mainTableError = $this->checkAndDelete($this->_tbl, $where);

return $mainTableError;
}

MKI-Miro

plus in in model folder in product.php in remove function move this

if (!$table->delete ($id)) {
$ok = FALSE;
}


to this position

if (!$votes->delete ($id, 'virtuemart_product_id')) {
$ok = FALSE;
}

if (!$table->delete ($id)) {
$ok = FALSE;
}

// delete plugin on product delete
// $ok must be set to false if an error occurs
VmConfig::importVMPlugins ('vmcustom');
$dispatcher = JDispatcher::getInstance ();
$dispatcher->trigger ('plgVmOnDeleteProduct', array($id, &$ok));


simple isnt it?