我正在尝试以编程方式在magento中创建产品。此脚本适用于默认安装。但是当我尝试在我给出表前缀的magento安装中创建产品时,它显示以下错误。你能告诉我我需要做些什么改变吗?
完整性约束违规:1452无法添加或更新子行:外键约束失败(
databasename_magento
。catalog_category_product_index
,CONSTRAINTFK_CAT_CTGR_PRD_IDX_CTGR_ID_CAT_CTGR_ENTT_ENTT_ID
FOREIGN KEY(category_id
)参考catalog_category_entity
(`entity _)
$product = new Mage_Catalog_Model_Product();
$product->setSku($sku);
$product->setAttributeSetId(9);
$product->setTypeId('simple');
$product->setName('55');
$product->setCategoryIds(array(7)); # some cat id's, my is 7
$product->setWebsiteIDs(array(1)); # Website id, my is 1 (default frontend)
$product->setDescription('Gift card Full description here');
$product->setShortDescription('Gift Card Short description here');
$product->setPrice('200'); # Set some price
$product->setWeight(4.0000);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$product->setStatus(1);
$product->setTaxClassId(0); # My default tax class
$product->setStockData(array( 'is_in_stock' => 1, 'qty' => 9999 ));
$product->save();
答案 0 :(得分:1)
错误消息明确通知category_id
字段违反了外键完整性。
$product->setCategoryIds(array(7)); # some cat id's, my is 7
上述语句必须导致错误,因为您设置的值不在引用的父列catalog_category_entity (entity_id)
中。
将输入更改为父表中的现有值并运行。它应该工作。