Magento 2 reindex returns No such entity
Not sure if this is too late to add, but when I had this It appeared to be caused by some entity ids that needed to be set to 0.
This script worked for me:
SET FOREIGN_KEY_CHECKS=0;
UPDATE `store` SET store_id = 0 WHERE code='admin';
UPDATE `store_group` SET group_id = 0 WHERE name='Default';
UPDATE `store_website` SET website_id = 0 WHERE code='admin';
UPDATE `customer_group` SET customer_group_id = 0 WHERE customer_group_code='NOT LOGGED IN';
SET FOREIGN_KEY_CHECKS=1;
Hope it helps.
In addition to Raphael:
I Debugged this code with altering the exception constructor for debug purposes (either by xdebug or old dumps).
Alter the construct of the exception for now (the file is \lib\internal\Magento\Framework\Exception\NoSuchEntityException.php
)
$trace = debug_backtrace();
var_dump($trace[1]['class'] . '::' . $trace[1]['function'] . '('.$trace[1]['line'] .')';
die();
After that go up one stack and check the arguments given (the args are also available in the debug backtrace, but dumping magento objects isn't really browser friendly).
Came to the conclusion, that it was in the storeGroupRepository
where it couldn't find the group id. Dived into the store tables and saw a few lines in the store table that were referencing a store_group
that wasn't in the store_group
table(store group not exist in relevant table).
Removed the faulty store lines and the error has been gone ever since.
But, keep in mind that it can also be in a different table/other reason. for example website, store itself or store group in this case. So that is always a little search you have to perform yourself.
Also found out that there were quite a lot of references to these stores and also removed them manually in the db. This can be a tedious work but it will solve the problem.
And don't forget to remove your hacks.
Ok so the No such entity.
error message comes from \lib\internal\Magento\Framework\Exception\NoSuchEntityException.php
.
This class is a custom exception class of Magento and it is used a lot in Magento 2.
Whenever you see code like:
catch (NoSuchEntityException $e)
Or
throw NoSuchEntityException;
This class is being used.
The problem in your case is that it's hard to find out from where that exception is being thrown.
So to narrow it a bit, you can start debugging the two classes related to your indexes that fail:
Magento\Catalog\Model\Indexer\Category\Product
=> catalog_category_product indexMagento\Catalog\Model\Indexer\Product\Category
=> catalog_product_category index
Fortunately, the second one extends the first one so you've got one starting point.
You've got to keep in mind that each of this indexer class uses action rows classes to process the reindexing:
Magento\Catalog\Model\Indexer\Product\Category\Action\Rows
Magento\Catalog\Model\Indexer\Category\Product\Action\Rows
To me that's where you should start debugging as those action row classes are the direct classes which are reindexing those two indexes.
I really hope it will help you find your problem.