-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Database Error Updating Magento to version 2.4.3 on setup:upgrade #33770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @burgh8wp. Thank you for your report.
Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:
For more details, please, review the Magento Contributor Assistant documentation. Please, add a comment to assign the issue:
🕙 You can find the schedule on the Magento Community Calendar page. 📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket. 🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel ✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel |
I had the same issue and tried to remove the duplicated entries (same versions for update). I have a similar issue again after saving a category with different url in store scope: eg: @ihor-sviziev predicted it #25746 (review) for backward compatibility, I'm adding the issue for different scoped attribute. |
Hi @ihor-sviziev appreciate your reply. But in this instance, they have been there for years with no issue. What has changed in version 2.4.3 specifically where this is no longer acceptable? There are literally thousands of duplications in this table on this 1 site, where there are circa 20,000 individual records. So going through to manually delete is not an option. Also the data is there for a reason, so deleting will break the data on site somewhere. So a better solution is definitely needed. Even a script to run manually once to change/update the ID number? But is this ID number linked from other database tables? |
@ihor-sviziev done, but that's not enough (unfortunately). I could upgrade removing all the duplicates (manually, they were 70K, I don't think this can be a solution for everybody), but I couldn't update the category url in a store scope later, as from the INSERT query above. |
@magento give me 2.4-develop instance |
Hi @Thundar. Thank you for your request. I'm working on Magento instance for you. |
Hi @Thundar, here is your Magento Instance: https://85b60892c241a3d87d464e439dff008f-2-4-develop.instances.magento-community.engineering |
I'm investigating the second problem (the insert) as I'm not being able to reproduce it in a fresh installation. |
Did you create a script to remove the duplicates? If so, how did you handle the ID number change or did you just delete them? |
@burgh8wp, sorry, I don't have this issue, can't help you with fixing that :( |
@burgh8wp i strongly suggest NOT to do this in a production environment. Edit: this query solved also the INSERT issue I quoted before. |
@Thundar fix unfortunately didn't resolve issue for me. etc/db_schema.xml
Next generate whitelist: |
@MajorKuprich i suggest you not to remove the constraint. Instead, you should find out what is blocking your table, and the cause of the ids duplicated. |
I can confirm this issue too, trying to upgrade from 2.4.2-p1 to 2.4.3.
@Thundar trick didn't work for me either, after running the SQL statement, I got a new error when running the command
|
I have the Same Error after upgrade Magento form 2.4.1 to Magento 2.4.3 |
@mahmoudbeikhit you can use solution like this #33770 (comment) |
BTW, it feels like the declarative schema missing the feature removing duplicates like in regular MySQL connection during adding unique index magento2/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php Lines 2800 to 2816 in 7c6b636
@maghamed what do you think about it? |
This is probably due catalog_url_rewrite_product_category_backup table left over from the transaction in Thundar's trick. It has foreign keys set with the same names as the magento update tries to add. I ended up dropping the backup table and got setup to go through. |
Hi @engcom-Delta. Thank you for working on this issue.
|
@jsyvanne you are right. I'll edit my snippet to drop that table then. |
This issue can happen if the instance previously used the Data migration tool to import M1 data. Duplicate Irrelevant duplicated rows can be removed from
|
I simply emptied this table Then you can simply re-generate this table by disabling and enabling Anchor for the category. If you have many categories. This might not be the solution. |
@amenk I use this to prevent duplicates from both category save and csv import: #33770 (comment) |
It looks like this issue was caused by #34210 + in 2.4.3 was added a unique key on this table. The correct fix will be following:
START TRANSACTION;
CREATE TABLE catalog_url_rewrite_product_category_backup SELECT * FROM catalog_url_rewrite_product_category;
TRUNCATE TABLE catalog_url_rewrite_product_category;
INSERT INTO catalog_url_rewrite_product_category SELECT DISTINCT * FROM catalog_url_rewrite_product_category_backup;
COMMIT; After the successful upgrade and making sure everything works fine, remove the backup table DROP TABLE catalog_url_rewrite_product_category_backup;
|
Our catalog_url_rewrite_product_category appears to be corrupt and above queries which previously worked, appear to no longer be working. Hitting errors on duplicate keys as before, then when we truncate the data, we get errors relating to foreign keys and inability to run database table altering commands through setup:upgrade Any advice/pointers on where to go to repair the database or regenerate? The core issues will have stemmed from the data (database) being migrated from M1 to M2 circa 4 years ago we believe. The site operated fine up to version 2.4.3 of Magento though. The solutions from @Thundar and @ihor-sviziev listed above previously worked for us earlier this year, but no longer working.... still using Magento version 2.4.3 Looks like this has been fixed in 2.4.4 ? https://devdocs.magento.com/guides/v2.4/release-notes/open-source-2-4-4.html Referencing below part: "Data patches can no longer ignore a table’s unique constraints and insert duplicate values into a MySQL database table. Previously, patches could insert duplicate values, which corrupted the database." |
@burgh8wp: the section you reference in the release notes is not the same as what is being discussed here (it has to do with #32283). So don't expect this to be fixed in 2.4.4 (at least not by what you reference). In this issue here, an attempt is made to try to make the I'm afraid that in your case, you'll probably want to manually search for duplicated Also, maybe check out the solution from #34791, it sounds related to this issue. But it looks like this one isn't included in Magento 2.4.4 yet. So it's probably scheduled for Magento 2.4.5. |
Ok, thanks for your reply @hostep In our case, there isn't actually any duplicated ID's. When you search by the ID number it references in the table, there is only 1 entry with that ID. It's the lack of Primary key in the table that seems to be the initial issue. We can truncate the table, then add the primary key, but on import of the data back into the table, we get the foreign key error. I suspect it's just an issue with our instance and the migration from M1 causing something wrong there. So really just looking for a solution to be able to delete the table and regenerate it correctly. Which seems to be tricky. Any ideas appreciated for that? |
Which foreign key gives you problems? It might reference product id's, or category id's or url_rewrite id's that no longer exist maybe? Also, if you don't care about potential SEO impact, you can try to run this tool to find potential incorrect data in your categories & products, which you then need to fix somehow, and then to run a tool like this to regenerate all url rewrites (which can also automatically fix category url_path issues the first tool might show you). Maybe that fixes your issues once and for all 🤞 |
Thanks @hostep feedback appreciated. Will look into those tools. Have deciphered it is definitely a lingering issue from M1 to M2 migration.
The old M1 database had a prefix for all tables, and I think it has muddled up the foreign keys somewhere along the lines, but it just seems to be this catalog_url_rewrite_product_category table only that is affected with any issue. |
@hostep you asked earlier for foreign key error which we have been able to replicate again today. If we truncate the table, and run setup:upgrade we get below error output:
Then went back and manually added Primary key to url_rewrite_id field, and error is same as above without that part mentioned. Then changed the Character set and Collation to what it's trying to do in above query, that part saves fine. On setup:upgrade the error is as above, without those parts mentioned. When we manually try to run that query directly on the DB, we get below error: 1005 - Can't create table Now stumped on why that's occurring......? |
@hostep we are making progress here, but just wanted to clarify if this issue will continue in 2.4.3 or 2.4.4 after we have fixed. In 2.4.4 will Magento still create duplicate url_rewrite_id in the catalog_url_rewrite_product_category table? |
Hi @burgh8wp, sorry for the lack of feedback but it's a complicated issue you are having and I don't have the time to dive into this. I have no idea if in Magento 2.4.4 you can still have insertion of duplicated entries. But as mentioned before, take a look at #34791, which sounds related, unfortunately that fix is not included yet in Magento 2.4.4 and will probably get included in 2.4.5, but maybe if that fix helps in your case, you can try to apply it as a patch? Again: I'm not sure if that will resolve your issue, this is only guessing. |
Appreciate the reply. It is the same issue, but don't see a patch there? Quite a lengthy article though, but can't see the actual solution..... Will just continue to manually fix as best we can until released in 2.4.5 I think |
You can click on the Files Changed tab in that Pull Request, to see the code changes 😉 |
Thanks @hostep but that patch as I mentioned doesn't look to be a solution for the (latest) issue we are experiencing. We cleaned up the duplicated, managed to complete setup:upgrade again. But the issue continues, and the core reason seems to be that Magento is creating the url_rewrite_id for an ID that is already in the table, and is not generating new ones starting off from the previous latest one. So for example, it should be auto incrementing around 118669 as the next new ID to go in, but is trying to add various that start 117xxx which suggests it has wrong stamp on where to start the ID? As far as I can see, the patch doesn't address any of this? |
It's the catalog_url_rewrite_product_category I was referring to, the url_rewrite table already has "Auto Increment" enabled. But the former table doesn't. Looked through all our Magento installs including default blank install, and seems catalog_url_rewrite_product_category always has "Auto Increment" disabled. But would make sense to have it enabled? Will test on dev site shortly, but any issues changing this? Will a future Magento upgrade change it back? |
No, you definitely do not want an auto increment on |
Ok, I see that structure now. If understanding the patch correctly here - #34791 All it's doing is essentially allowing duplicates to be created for url_rewrite_id for the catalog_url_rewrite_product_category table? That's it? Assuming that is not going to cause other further issues down the line? |
Hi try this solution and works for me: For me the problem was that the afterReplace function in Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage added duplicate url rewrites to the catalog_url_rewrite_product_category table. Since a while there is a primary key on that table so this gives an error. The solution is to delete the existing url_rewrites first, the function removeMultiple already exists. Overwrite Magento\CatalogUrlRewrite\Model\Category\Plugin\Storage and add this line:
So it becomes like this:
can someone more experienced than me verify? |
For my solution to rebuild
$om = \Magento\Framework\App\ObjectManager::getInstance();
$categoryCollectionFactory = $om->get(\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory::class);
$urlRewriteHandler = $om->get(\Magento\CatalogUrlRewrite\Observer\UrlRewriteHandler::class);
$urlRewriteBunchReplacer = $om->get(\Magento\CatalogUrlRewrite\Model\UrlRewriteBunchReplacer::class);
$categoryCollection = $categoryCollectionFactory->create()->addAttributeToSelect('*')->addIsActiveFilter();
foreach ($categoryCollection as $category) {
echo "Category [" . $category->getId() . "] Processing ...\n";
// for $category->dataHasChangedFor('is_anchor') === true
$category->setOrigData('is_anchor', !$category->getData('is_anchor'));
$productUrlRewriteResult = $urlRewriteHandler->generateProductUrlRewrites($category);
$urlRewriteBunchReplacer->doBunchReplace($productUrlRewriteResult);
} Ref: vendor/magento/module-catalog-url-rewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php magento2/app/code/Magento/CatalogUrlRewrite/Observer/CategoryProcessUrlRewriteSavingObserver.php Lines 123 to 124 in 2e45de7
|
Same issue here, upgrading from 2.3.7-p3 to 2.4.5: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2918522' for key 'catalog_url_rewrite_product_category.PRIMARY', query was: ALTER TABLE |
Problem here in our case was that our customer had duplicate entries in the url_key field from products. This issue needs to be fixed, so no duplicate urls can exist. This is what we did:
With output let the customer fix there url keys Obv we did this on a test env, and after the customer fixed the issues with the url keys, on production. |
@magento I am working on it |
@magenest-dev We also have a similar issue. looks like the two same |
After updating Magento from the version. 2.4.2-p1 to 2.4.3 during setup:upgrade command we see below error:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '106852' for key 'PRIMARY', query was: ALTER TABLE
catalog_url_rewrite_product_category
ADD CONSTRAINT PRIMARY KEY (url_rewrite_id
)The problem is this table holds around 20,000 records, and looks to be hundreds, potentially over 1000 instances of a duplicate ID for url_rewrite_id which stems from Magento 2.4.3 looking to force a primary key onto the database table catalogu_url_rewrite_product_category
Preconditions (*)
Steps to reproduce (*)
Expected result (*)
Actual result (*)
[ ] Severity: S0 _- Affects critical data or functionality and leaves users without workaround.
** Changing back to 2.4.2-p1 does not give this error output and setup:upgrade completes fine.
The text was updated successfully, but these errors were encountered: