0

i have that error when try to insert new product programatically

string '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_AT

My code is

               Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
            $product = new Mage_Catalog_Model_Product();
                //if(!$product->getIdBySku($single_product['id'])){

            try{
            $product

                ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
                ->setAttributeSetId(9) //ID of a attribute set named 'default'
                ->setTypeId('simple') //product type
                ->setCreatedAt(strtotime('now')) //product creation time
                ->setUpdatedAt(strtotime('now')) //product update time

                ->setSku('214dd21d') //SKU
                ->setName('test name') //product name

                ->setStatus(1) //product status (1 - enabled, 2 - disabled)
                ->setTaxClassId(1) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
                ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
                ->setManufacturer(28) //manufacturer id
                ->setColor('red')
                ->setNewsFromDate(11/10/2014) //product set as new from
                ->setCountryOfManufacture('US') //country of manufacture (2-letter country code)

                ->setPrice(2000) //price in form 11.22
                ->setCost(2000) //price in form 11.22
                // ->setSpecialPrice($single_product['price']) //special price in form 11.22
                // ->setSpecialFromDate('06/1/2014') //special price from (MM-DD-YYYY)
                // ->setSpecialToDate('06/30/2014') //special price to (MM-DD-YYYY)
                // ->setMsrpEnabled(1) //enable MAP
                ->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
                //->setMsrp(99.99) //Manufacturer's Suggested Retail Price

                ->setMetaTitle('test title')
                ->setMetaKeyword('test keyword')
                ->setMetaDescription('test meta ')

                ->setDescription('test')
                ->setShortDescription('test')

                //->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization
                //->addImageToMediaGallery('media/catalog/product/1/0/10243-1.png', array('image','thumbnail','small_image'), false, false) //assigning image, thumb and small image to media gallery

                ->setStockData(array(
                                   'use_config_manage_stock' => 0, //'Use config settings' checkbox
                                   'manage_stock'=>1, //manage stock
                                   'min_sale_qty'=>1, //Minimum Qty Allowed in Shopping Cart
                                   'max_sale_qty'=>1, //Maximum Qty Allowed in Shopping Cart
                                   'is_in_stock' => 1, //Stock Availability
                                   'qty' => 1 //qty
                               )
                )

                ->setCategoryIds(array(3)); //assign product to categories
            $product->save();
            die('done');
          }catch(Exception $e){

            die(var_dump($e->getMessage()));
            //Mage::log($e->getMessage());

           }
       //}

1 Answer 1

1

Are you sure that the attribute set ID is 9 and that this attribute set was created correctly?

The error seems to indicate otherwise. This foreign key is responsible for ensuring the integrity between the catalog_product_entity table and the eav_attribute_set table. In a new installation the ID for the Default product attribute set is 4.

Please re-check the attribute sets in the table eav_attribute_set called Default . There are multiple attribute sets with this name. They are related to different entity types (categories, products, ...). Check the column entity_type_id. The ID in this column refers to another table eav_entity_type which defines the entity types category, product, ...

An example from a vanilla CE 1.9 install:

  • In table eav_entity_type, there is an entry with the entity_type_code catalog_product. This row has the ID 4.
  • Checking eav_attribute_set, you'll find an entry with the entity_type_id 4 and the attribute_set_name Default. In my test installation this row has the ID 4.
  • So you'd have to take this ID and use it for the product definition: $product->setAttributeSetId(4);
0

Your Answer

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

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