VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: MKI-Miro on September 26, 2019, 12:45:51 PM

Title: Cannot remove product if there is FK
Post by: MKI-Miro on September 26, 2019, 12:45:51 PM
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
Title: Re: Cannot remove product if there is FK
Post by: StefanSTS on September 26, 2019, 13:28:31 PM
Can you explain that again please.
Title: Re: Cannot remove product if there is FK
Post by: MKI-Miro on September 26, 2019, 13:51:30 PM
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
Title: Re: Cannot remove product if there is FK
Post by: GJC Web Design on September 26, 2019, 17:20:13 PM
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
Title: Re: Cannot remove product if there is FK
Post by: Studio 42 on September 26, 2019, 17:56:35 PM
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
Title: Re: Cannot remove product if there is FK
Post by: MKI-Miro on September 27, 2019, 09:55:28 AM
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....
Title: Re: Cannot remove product if there is FK
Post by: MKI-Miro on October 14, 2019, 09:33:34 AM
is it really so hard to change order of delete?
Title: Re: Cannot remove product if there is FK
Post by: StefanSTS on October 14, 2019, 10:40:20 AM
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
Title: Re: Cannot remove product if there is FK
Post by: AH on October 14, 2019, 15:33:57 PM
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
Title: Re: Cannot remove product if there is FK
Post by: 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...
Title: Re: Cannot remove product if there is FK
Post by: StefanSTS on October 16, 2019, 09:51:52 AM
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?

Title: Re: Cannot remove product if there is FK
Post by: GJC Web Design on October 16, 2019, 11:08:02 AM
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..
Title: Re: Cannot remove product if there is FK
Post by: MKI-Miro on October 16, 2019, 11:38:20 AM
So you dont agree that code, which first deletes product and later references to this product, is wrong? really?
Title: Re: Cannot remove product if there is FK
Post by: MKI-Miro on October 16, 2019, 11:48:16 AM
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;
}
Title: Re: Cannot remove product if there is FK
Post by: MKI-Miro on October 16, 2019, 12:06:35 PM
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?
Title: Re: Cannot remove product if there is FK
Post by: StefanSTS on October 16, 2019, 12:46:22 PM
Well, it might be "wrong" from your perspective. It works for everyone else in a normal production environment.

If you just demand changes you are in the wrong place here.
Try Shopify or so.

I am not speaking for the VirtueMart team since I am someone outside of VirtueMart who makes money with VirtueMart and has suggested changes for the last few years. I had to test a lot, I found a lot of bugs, I corrected stuff, I tested it. And you benefit from my work.

And you behave like it is the duty of the VirtueMart team to fix your problems immediately.

Well, good luck
Stefan
Title: Re: Cannot remove product if there is FK
Post by: GJC Web Design on October 16, 2019, 12:55:59 PM
Quotesimple isnt it?

looks eminently simple and if you have fully tested all scenarios u can add it to feature requests or what ever
Title: Re: Cannot remove product if there is FK
Post by: MKI-Miro on October 16, 2019, 14:23:33 PM
Quote from: StefanSTS on October 16, 2019, 12:46:22 PM
And you behave like it is the duty of the VirtueMart team to fix your problems immediately.

no this is not ONLY fix for my problem, this is fix of virtuemart code for everyone.

next time you go to company interview, tell them that you first delete product and later you delete references to this product

do you think they will hire you? :)
Title: Re: Cannot remove product if there is FK
Post by: StefanSTS on October 16, 2019, 19:00:01 PM
If there is a company and they want to hire you to fix a few problems that are stopping a shop from selling items and you tell them you first want to fix a code error that doesn't do any harm to anyone, and do the important fixes later, because the clerk from accounting thinks, that the error is a major flaw, they would hire you?

I see your intentions, very nice, that you want to have the core fixed, you are totally right, that is important for everyone. For you it is most important to fix your own problem right now, and what you can do to do that as fast as possible in the core, is what is decribed above by GJC. With making false comparisons you waste your time and don't motivate others to help you.

So my suggestion is, relax, test, try the nice way.
Title: Re: Cannot remove product if there is FK
Post by: MKI-Miro on October 19, 2019, 07:45:44 AM
I dont know what problems are you talking about (I am sorry I am only customer), but here is stable version published, isnt it?

And yes in this case these problems are more important then mine, but you should not have such problems in stable version....

Btw few days I am testing mine changes and everything seems to be wroking correctly
Title: Re: Cannot remove product if there is FK
Post by: StefanSTS on October 19, 2019, 09:54:59 AM
Quote from: MKI-Miro on October 19, 2019, 07:45:44 AM
I dont know what problems are you talking about (I am sorry I am only customer), but here is stable version published, isnt it?
--> If you don't understand how I replied to your false comparison with one that fits more, you don't want to understand anyway, that is fine.
--> I understand you are a user of the free VirtueMart extension. Or are you a customer of iStraxx and have paid money for support and developement, in that case you should communicate directly with them. Things will happen much faster.
--> Yes, there is a stable version, and that code has not given anyone else problems.

Quote from: MKI-Miro on October 19, 2019, 07:45:44 AM
And yes in this case these problems are more important then mine, but you should not have such problems in stable version....
--> Again, there is a stable version, and that code has not given anyone else problems. It is just a problem for you, because you want to extend functionality.

Quote from: MKI-Miro on October 19, 2019, 07:45:44 AM
Btw few days I am testing mine changes and everything seems to be wroking correctly
--> If that is all successful you should post a report on how you tested the scenarios, and if that if fine for the core developers, they might accept the change.

Stefan