Laravel SQLSTATE[23000]: Integrity constraint violation: 1452 - php

I am facing an integrity constraints issue when adding a new row to a table has relation with another table (children and visits tables) every child has many visits.
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (vaccine.visits, CONSTRAINT visits_ch_id_foreign FOREIGN KEY (ch_id) REFERENCES childern (id)) (SQL: insert into visits (ch_id, ce_id, emp_id, next_visit, notes, updated_at, created_at) values (202151-15, 1, 2, 2021-05-21, this is a test, 2021-05-17 07:30:48, 2021-05-17 07:30:48))
I am understanding the error message that should the value in the foreign key (ch_id) in the visits table should be found in the primary key (id) in the children key.
The problem is the value already exists in the primary table but I am getting the same error
Here is my code and screenshot for the database
$visit=new Visit();
$visit->ch_id=$child_id;
$visit->ce_id=1;
$visit->emp_id=2;
$visit->next_visit=$request->input('next_visit_date');
$visit->notes=$request->input('visit_notes');
$result=$visit->save();
I am testing on value 202151-15 and it already exists on the children table as following:

Related

how to fix Integrity constraint violation: 1452 Cannot add or update a child row?

I have been stuck on this error
Failed to save dataSQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign
key constraint fails (ticketdatabase.ticket, CONSTRAINT ticket_ibfk_4 FOREIGN KEY (EventId) REFERENCES event
(id) ON DELETE CASCADE ON UPDATE CASCADE)"
my sql code is
INSERT INTO ticket (TicketId,EventId,StatusId,Price,UserId,TypeId) VALUES (?,?,?,?,?,?);
the ticket table has a relationship with event table. and the value that i insert actually is already exists on event table but still shows this error.
when i manually insert in php myadmin it inserts fine. but trough the code it raise the error. any help please

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update

i have this problem :
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (tutoblog.post_category, CONSTRAINT fk_category FOREIGN KEY (category_id) REFERENCES category (id) ON DELETE CASCADE ON UPDATE CASCADE)
You're trying to insert/update a table with a foreign key constraint.
The field category_id must be set to an id that exists in you table category.
So make sure you create your category first and then link it in your entry.

Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails

I wanted to delete a user profile, when the last logout time was 2 years ago and there is no current activities were made during that time.
This is the problem encountered when i run the code:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or
update a parent row: a foreign key constraint fails
(spwtd-test.training_user_answers, CONSTRAINT
training_user_answers_training_user_id_foreign FOREIGN KEY
(training_user_id) REFERENCES training_users (id))
this is the snippet code in loginController:
if ($completed = UserSyllabus::where('is_completed',1)->first())
{$aa = UserAnswer::where('training_user_id')
$userss = User::where('login_id', $request->login_id)
->where( 'last_logout', '<', Carbon::now()->subYears(2))
->delete();}
You are trying to delete a User but an entry in your table UserAnswer is bind to User ( using the foreign key training_user_id ).
You need to delete the UserAnswer, before deleting the User itself.

Integrity constraint violation – yii\db\IntegrityException

in Yii framework i am facing an issue with deleting a record, info table contains a foriegn key which is linked with other tables. i am deleting the records sequence wise by deleting record from info table in last. Kindly help me solve this issue.
$model = StuMaster::findOne($id);
$address = StuAddress::findOne($model->stu_master_stu_address_id);
$info = StuInfo::findOne($model->stu_master_stu_info_id);
$pastacademics = StuPastacademics::findOne(['pastacad_studenid' => $info->stu_unique_id]);
$guardians = StuGuardians::findAll(['guardia_stu_master_id' => $model->stu_master_id]);
$pastacademics->delete();
$model->delete();
$info->delete();
Integrity constraint violation – yii\db\IntegrityException
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (whizzee_dihedblat.stu_info, CONSTRAINT stu_info_ibfk_3 FOREIGN KEY (stu_info_stu_master_id) REFERENCES stu_master (stu_master_id))
The SQL being executed was: DELETE FROM stu_master WHERE stu_master_id=61

Find which rows are causing Foreign Key Constraint Failure Error

I am getting the following error when i try and insert something into my database:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
(`database_name`.`catalog_category_entity_varchar`, CONSTRAINT `FK_CAT_CTGR_ENTT_VCHR_ENTT_ID_CAT_CTGR_ENTT_ENTT_ID` FOREIGN KEY (`entity_id`) REFERENCES `catalog_category_entity` (`entity_i)'
Am i right in thinking this means:
there is an entity_i in catalog_category_entity that is not found in
entity_id in catalog_category_entity_varchar
Is there an SQL query i can do on the database that can find what is causing this?
The database is Magentos and this error comes up simply when i create a category programmatically:
$category = Mage::getModel('catalog/category');
$category->setName($design);
$category->setPath($path);
$category->save();

Categories