Magento – Magento 2 Setup:Upgrade giving error of Cannot add foreign key constraint

indexingmagento2reindexsetup-upgrade

I have moved my website files and DB from one server to another.

Now, When I am running setup:upgrade command. I am getting below error:

Updating modules:
Schema creation/updates:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint, query was: ALTER TABLE quote_item MODIFY COLUMN item_id int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT "Item Id", ADD CONSTRAINT PRIMARY KEY (item_id), ADD CONSTRAINT QUOTE_ITEM_PARENT_ITEM_ID_QUOTE_ITEM_ITEM_ID FOREIGN KEY (parent_item_id) REFERENCES quote_item (item_id) ON DELETE CASCADE, ADD CONSTRAINT QUOTE_ITEM_QUOTE_ID_QUOTE_ENTITY_ID FOREIGN KEY (quote_id) REFERENCES quote (entity_id) ON DELETE CASCADE, ADD CONSTRAINT QUOTE_ITEM_STORE_ID_STORE_STORE_ID FOREIGN KEY (store_id) REFERENCES store (store_id) ON DELETE SET NULL, ADD INDEX QUOTE_ITEM_PARENT_ITEM_ID (parent_item_id), ADD INDEX QUOTE_ITEM_PRODUCT_ID (product_id), ADD INDEX QUOTE_ITEM_QUOTE_ID (quote_id), ADD INDEX QUOTE_ITEM_STORE_ID (store_id)

Please help to get rid of this error.

Best Answer

You need to run this query:

Select item_id from quote_item where parent_item_id is not null and parent_item_id in (select item_id from quote_item)

After that, the result will be updated and you need to fix it to parent_item_id = null.

Leave a comment if it works!

Related Topic