Find which rows are causing Foreign Key Constraint Failure Error - php

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();

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

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

This error show every..*.**please help me for the exact answer for this qyery...
SQLSTATE[23000]: Integrity constraint violation: 1452
Cannot add or update a child row: a foreign key constraint fails
(mydawai.cataloginventory_stock_item, CONSTRAINT
FK_CATINV_STOCK_ITEM_PRD_ID_CAT_PRD_ENTT_ENTT_ID FOREIGN KEY
(product_id) REFERENCES catalog_product_entity (entity_id) ON DELETE
CA)
i'm importing the 20 to 50 thousand csv product they will show an error but if part into the 500 hundred -500 hundred list than some of the product will be importing and someone showing error of sql 23000. Integrity constraint violation: 1452 ?
You need to remove any special chacarter in your Csv File. like ©, '', º
Thats works for me !
its means that there is a foreign key that is not satisfied (ie you need it to exist but it doesnt)
catalog_product_entity must have and entry for entity_id which is then referenced as product_id in cataloginventory_stock_item.

Categories