I'm trying to import my products form a .csv. The import script for the simple products works like a charm, but the script for the configurable product returns the following error:

 SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`sendb`.`catalog_product_super_link`, CONSTRAINT `FK_CAT_PRD_SPR_LNK_PRD_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DEL)


if(($handle = fopen("werkbladvoorraad.csv", "r")) !== FALSE){
    while(($data = fgetcsv($handle, 1000, ";")) !== FALSE){
            $product = Mage::getModel('catalog/product');
            $product ->setStoreId(1)
                     ->setName($data[1] . " " . $data[10])

             $product ->setCategoryIds(array(2,11,12, $categories[$data[13]]))
             $attribute_ids = array(139);

             $sql = "SELECT * FROM sku WHERE sku LIKE '$data[2]%' ";

             $result = $con->query($sql);

             $configurableProductsData = array();

             while($row = mysqli_fetch_array($result)){
                echo $row['sku']."<br>";
                $sproduct = Mage::getModel('catalog/product')->load($row['product_id']);
                $configurableProductsData[$sproduct->getId()] = $sproduct->getData();

             $productTypeInstance = $product->getTypeInstance();
             $attributes_array = $productTypeInstance->getConfigurableAttributesAsArray();

             foreach($attributes_array as $key => $attribute_array){
                $attributes_array[$key]['use_default'] = 1;
                $attributes_array[$key]['position'] = $y;

                    $attributes_array[$key]['label'] = $attribute_array['frontend_label'];
                    $attributes_array[$key]['label'] = $attribute_array['attribute_code'];

            $product->setStockData(array('use_config_manage_stock' => 1, 'is_in_stock' => 1, 'is_salable' => 1));

                echo "yay";
                echo "nay";


Note i've stored the information from the simple products in the database so that explains the query in the script.

Edit I found the error occurs on this line:

  • The error message tells you that SQL finds a foreign key contrain is failing. That means it probably has not much to do with your code, but more about in which order you try to import the configurable products. You have to make sure that all referenced tables and columns exist before you add contrains or them or data which references to them Jan 23 '15 at 14:41
  • Duplicate of magento.stackexchange.com/questions/14981/… Jan 23 '15 at 15:49

The error message tells you that the child product you are refencing doesn't exist.

I assume you made an error while gathering the child product data. The line $sql = "SELECT * FROM sku WHERE sku LIKE '$data[2]%' "; looks wrong - there is no database table "sku" in the magento default installation. Probably you mean catalog_product_entity. Then, you get the product id from the field entity_id, not product_id.

You should consider importing your products with ImportExport. See http://www.integer-net.com/2012/04/04/importing-products-with-the-import-export-interface/ for more details. It's much faster and very reliable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.