cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot Add New Product

Cannot Add New Product

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`ab6cbeda_tots`.`catalog_product_entity_varchar`, CONSTRAINT `CAT_PRD_ENTT_VCHR_ENTT_ID_CAT_PRD_ENTT_ENTT_ID` FOREIGN KEY (`entity_id`) REFERENCES `catalog_product_entity` (`entity_id`) ON DEL), query was: INSERT INTO `catalog_product_entity_varchar` (`attribute_id`,`store_id`,`value`) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)

Does anyone happen to have this kind of issue? Adding new product was working just fine before, until we migrated the site to new server and we have that error when adding new product.

 

Please let me know if someone knows how to fix it.

4 REPLIES 4

Re: Cannot Add New Product

Hi @zephro98 

 

To fix this, first check that this is indeed the issue. Backup your database and then run the following SQL to see if any product and/or category associations are incorrect:

# Determine incorrect product associations
SELECT * FROM `catalog_category_product` WHERE
product_id not in (select entity_id from catalog_product_entity);

# Determine incorrect categories
SELECT * FROM `catalog_category_product` WHERE 
category_id not in (select entity_id from catalog_category_entity);

If that returns a record, you'll know exactly which product is in a non-existent category or which category has a non-existent product. Run the following SQL to then remove those invalid records:

# Delete incorrect product associations
DELETE FROM `catalog_category_product` WHERE 
product_id not in (select entity_id from catalog_product_entity);

# Delete incorrect categories
DELETE FROM `catalog_category_product` WHERE 
category_id not in (select entity_id from catalog_category_entity);

Now log back into Magento Admin and reindex the Category Products index.

 

If issue resolve, please click on 'Kudos' & Accept as Solution!

Problem solved? Click Accept as Solution!

Re: Cannot Add New Product

@zephro98 

 

Please check your attribute Id exists in database table or not, it seems attribute id deleted but exists in another table.

 

Or for temp fix by this you can add products 

SET FOREIGN_KEY_CHECKS=0

 After that you can again do 

SET FOREIGN_KEY_CHECKS=1

Please take note this is not recommended solution but it will work for temp. 

Manish Mittal
https://www.manishmittal.com/

Re: Cannot Add New Product

Hello @Bhanu Periwal,

 

I've tried running the queries but I don't see any issues or incorrect associations.

Re: Cannot Add New Product

Hello @Manish Mittal 

 

I've tried running the query

SET FOREIGN_KEY_CHECKS = 0

But still didn't worked.