0

I am creating an admin module where the user import's a .xls file in magento, then the .xls file can be executed and the data inside has to be imported into the products table.

This is the error I get:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`catalog_product_entity`, CONSTRAINT `FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID` FOREIGN KEY (`attribute_set_id`) REFERENCES `eav_attribute_set` (`attribute_set_id`) ON DE), query was: INSERT INTO `catalog_product_entity` (`entity_type_id`, `attribute_set_id`, `type_id`, `sku`, `created_at`, `updated_at`) VALUES (?, ?, ?, ?, '2016-09-30 11:43:55', '2016-09-30 11:43:55')
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`catalog_product_entity`, CONSTRAINT `FK_CAT_PRD_ENTT_ATTR_SET_ID_EAV_ATTR_SET_ATTR_SET_ID` FOREIGN KEY (`attribute_set_id`) REFERENCES `eav_attribute_set` (`attribute_set_id`) ON DE), query was: INSERT INTO `catalog_product_entity` (`entity_type_id`, `attribute_set_id`, `type_id`, `sku`, `created_at`, `updated_at`) VALUES (?, ?, ?, ?, '2016-09-30 11:43:55', '2016-09-30 11:43:55')

And this is the code that creates the product in the controller:

foreach ($data as $value) {
                    $rand = rand(1, 9999);
                    try {
                        // $product = Mage::getModel('catalog/product');
                        $product = new Mage_Catalog_Model_Product();
                        $product->setAttributeSetId(9)
                                 ->setStoreId(Mage::app()->getStore()->getId())
                                 ->setTypeId('simple')
                                 ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                                 ->setTaxClassId(2)
                                 ->setCreatedAt(strtotime('now'))
                                 ->setSku($value[0] . $rand)
                                 ->setName($value[1])
                                 ->setDescription($value[2])
                                 ->setShortDescription($value[3])
                                 ->setWeight((int) substr($value[4], 0, -1))
                                 ->setPrice((float) substr($value[5], 1))
                                 // ->setCost((float) substr($value[5], 1))
                                 ->setStockData(array(
                                    'use_config_manage_stock' => 0,
                                    'manage_stock'               => 1,
                                    'min_sale_qty'               => 1,
                                    'max_sale_qty'               => (int) substr($value[6], 0, -1),
                                    'is_in_stock'                => 1,
                                    'qty'                        => (int) substr($value[6], 0, -1)
                                 ))
                                 // ->setColor((int) substr($value[7], 0, -1))
                                 ////////////////////////////////////// CUSTOM OPTIONS FOR TYPE
                                 ////////////////////////////////////// CUSTOM OPTIONS FOR GLASS
                                 ////////////////////////////////////// CUSTOM OPTIONS FOR ALCHOL
                                 ////////////////////////////////////// CUSTOM OPTIONS FOR GRAPES
                                 ////////////////////////////////////// CUSTOM OPTIONS FOR BOTTLE FORMAT
                                 ////////////////////////////////////// CUSTOM OPTIONS FOR BOTTLE FORMAT
                                 // ->setCountryOfManufacture($value[14])
                                 // ->setNewsFromDate(strtotime('now'))
                                 // ->setNewsToDate(strtotime('now'))
                                 ->setStatus(1)
                                 ->setCategoryIds(array(2, 3))
                                 ->setWebsideIds(array(1));
                        $product->save();

                        var_dump($product);
                        die(0);
                    } catch (Exception $e) {
                        Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                    }
                }

                $wineModel->setId($this->getRequest()->getParam('id'))
                            ->setStatus(1)
                            ->setExecuted_at(now())
                            ->save();
            }
4
  • Just to be sure, check your eav_attribute_set table to make sure that an attribute set with id 9 exists. Commented Sep 30, 2016 at 13:10
  • It doesn't exist, how should I insert it into the table? I don't think I have to insert it manually, am I missing something in my code? Commented Sep 30, 2016 at 13:13
  • You have to use an attribute set id which exists. The default attribute set for products is has id 4. What is the reason for using 9? Commented Sep 30, 2016 at 13:22
  • Thank you, this solved my problem. Do you want to post your comment as an answer so I can close the question? Commented Sep 30, 2016 at 13:30

1 Answer 1

0

Just to be sure, check your eav_attribute_set table to make sure that an attribute set with id 9 exists.

You have to use an attribute set id which exists. The default attribute set for products is has id 4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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