Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
 
luiskha
Jr. Member
 
Total Posts:  4
Joined:  2011-02-14
 

I’m having the following error when I try to save two new fields I created (Country and Company) in the Customer Address (Billing).  The information for these fields are provides from the Customer form: It’s like this how i try to save it.

$customerAddressId = Mage::getSingleton(’customer/session’)->getCustomer()->getDefaultBilling();
$address = Mage::getModel(’customer/address’)->load($customerAddressId);
$address->setCountry($customer->getCountry());
$address->setCompany($customer->getCompany());

$address->save();

At the moment to make the post i have this database error: 

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento2`.`customer_address_entity`, CONSTRAINT `FK_CUSTOMER_ADDRESS_CUSTOMER_ID` FOREIGN KEY (`parent_id`) REFERENCES `customer_entity` (`entity_id`) ON DELETE CASCADE ON UPDATE CASCADE)

I read that I would resolve it if I change the all tables engine for innodb like this:

alter table the_table engine=innodb;

but doesn’t work

Anyone can help me? I really need it ....

 
Magento Community Magento Community
Magento Community
Magento Community
 
luiskha
Jr. Member
 
Total Posts:  4
Joined:  2011-02-14
 

I found my mistake. When I tried to load the default address in this part of the code:

$customerAddressId = $customer->getDefaultBilling()
$address = Mage::getModel(’customer/address’)->load($customerAddressId);

The error message I’ve got is because I don’t have anything to be saved (Default address doesn’t exist yet). The solution was to create one object customer/address using this code:

if (!$address->getId()) {//If doesn’t existe one
$address = Mage::getModel(’customer/address’);
$address->setIsDefaultBilling(true);
$address->setCustomerId($customer->getId());
}

I created it and I’ll be able to load one address for be saved with the new attributs.

$address->setCountryId($customer->getCountry());
$address->setCompany($customer->getCompany());
$address->save();

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top