AlGallaf
AlGallaf

Reputation: 635

MySQL Error: #1452 - Cannot add or update a child row: a foreign key constraint fails

enter image description here

I am able to successfully insert a row in the business table but not the business_contact table. Whenever I try to insert a row in the business_contact table I get the following error:

#1452 - Cannot add or update a child row: a foreign key constraint fails

Although the row that I'm attempting to insert into the business_contact table has a BusinessID that already exists in the business table.

business_contact failed insert statement

INSERT INTO business_contact(BusinessID, BusinessContactTypeID, BusinessContactData) VALUES (1, 1, '0097336031000');

Upvotes: 1

Views: 10887

Answers (1)

vee
vee

Reputation: 38645

You should check the following in your business and contact_type_lookup table: 1. business table has BusinessID value that you are trying to insert into business_contact 2.contact_type_lookuptable hasContactTypeIDvalue that you are trying to insert into business_contact

This error is related to either missing BusinessID and or missing ContactTypeID. To verify this please select the BusinessID record from business table and ContactTypeID from contact_type_lookup table.

For example: If you are executing the following query:

insert into business_contact(BusinessContactID, BusinessID, BusinessContactTypeID, BusinessContactData) values ('1', '2', '3', 'This is the business contact data');

Then, ensure that the following queries returns valid results:

select BusinessID from business where BusinessID = 2;

select ContactTypeID from business_contact_type where ContactTypeID = 3;

Upvotes: 2

Related Questions