Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anp24's avatar
Level 1

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

I have 3 tables

applicant_profiles - having id as primary key user_profiles - having id as primary key

users table - having applicant_id and user_id as Foreign keys ,

now if I insert into the users table , I encountered the following error.

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - FK_Applicant_ID

I am sending applicant_id as 0 and user_id from user_profiles table

0 likes
3 replies
tykus's avatar

I am sending applicant_id as 0

Do you have a record in the related table with this id?

faabsen's avatar

It looks like your trying to add invalid data to the database. Because of your foreign key constraint you can not add non-existing keys to your users table. Therefore you should use NULL as the applicant_id

anp24's avatar
Level 1

applicant_id with NULL value solves the error

Please or to participate in this conversation.