SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento2`.`catalog_product_entity_datetime`, CONSTRAINT `CAT_PRD_ENTT_DTIME_ENTT_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`entity_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DELETE CAS), query was: INSERT INTOcatalog_product_entity_datetime(attribute_id, store_id, entity_id, `value`)VALUES(:attribute_id0,:store_id0,:entity_id0,:value0)ON duplicate KEY UPDATE `value` = VALUES(`value`)
The problem
Cron schedule return status error
when processing job catalog_product_attribute_value_synchronize
with the error message above.
The reason is some products are deleted without deleting the corresponding catalog_product_entity_*
records. So they contain some records that have nonexistent entity_id
.
It may happen with these tables:
- catalog_product_entity_datetime
- catalog_product_entity_decimal
- catalog_product_entity_int
- catalog_product_entity_text
- catalog_product_entity_varchar
The solution
Remember always backup before you touch the database.
In this case, it’ safe to delete junk catalog_product_entity_*
records:
delete from catalog_product_entity_datetime where entity_id not in (select entity_id FROM catalog_product_entity);
Replace catalog_product_entity_datetime
with the corresponding table name in the error message. Again, always backup first.