How can I fix an Integrity constraint violation about 'linked_product_id' (when importing)
-
16-10-2019 - |
Question
During import I get errors like
PHP Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(`foo`.`catalog_product_link`, CONSTRAINT
`FK_CAT_PRD_LNK_LNKED_PRD_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY
(`linked_product_id`) REFERENCES `catalog_product_entity` (`entity_id`)'
in lib/Zend/Db/Statement/Pdo.php:228
I already changed Mage_ImportExport_Model_Import_Entity_Product::_saveLinks()
for debugging the single inserts:
if ($linkRows) {
foreach($linkRows as $row) {
$adapter->insertOnDuplicate(
$mainTable,
$row,
array('link_id')
);
}
And now I get linked_product_id' cannot be null'
The full import stops because of this error.
Solution
You can add some debug code in \Mage_ImportExport_Model_Import_Entity_Product::_saveLinks
to track down this problem and to avoid, that the full import stops:
if ((isset($this->_newSku[$linkedSku]) || isset($this->_oldSku[$linkedSku]))
&& $linkedSku != $sku) {
if (isset($this->_newSku[$linkedSku])) {
$linkedId = $this->_newSku[$linkedSku]['entity_id'];
} else {
$linkedId = $this->_oldSku[$linkedSku]['entity_id'];
}
$linkKey = "{$productId}-{$linkedId}-{$linkId}";
// [BEGIN CHANGE] - Comment: Debug
if ($linkedId == null) {
Mage::logException(
new Exception(
sprintf('Debug - WARNING: Orphaned link skipped: From SKU %s (ID %d) to SKU %s, Link type id: %d',
$sku, $productId, $linkedSku, $linkId)
)
);
continue;
}
// [END]
This will log an exception with detailed information about the products and links the error is about.
In my case there were errors with the products referenced as related products. So those products were skipped during import and Magento generated links to "null"
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange