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

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.

Related

Laravel SQLSTATE[23000]: Integrity constraint violation: 1452

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:

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.

MYSQL and PHP: How to add mysql constraint but proceed anyway if not found

I have a code where I need a foreign key check disabled when updating. But I still need it to check for the foreign key but proceed anyways if the key is not found. The installation keeps breaking due to this.
$installthis->run("
ALTER TABLE `{$this->getTable('sweets/results')}`
ADD CONSTRAINT `FK_SWEETS_RESULTS_SWEET_ID_SWEETS_ID` FOREIGN KEY (`sweet_id`)
REFERENCES `{$this->getTable('sweets/sweets')}` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
ALTER TABLE `{$this->getTable('sweets/results_values')}`
ADD CONSTRAINT `FK_SWEETS_RESULTS_VALUES_RESULT_ID_SWEETS_RESULTS_ID` FOREIGN KEY (`result_id`)
REFERENCES `{$this->getTable('sweets/results')}` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
");
What do I need to change or add if I want it to proceed?
Edit:
The error states that
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
(sweets_database.sweets_results, CONSTRAINT
FK_SWEETS_RESULTS_SWEET_ID_SWEETS_ID FOREIGN KEY (sweet_id)
REFERENCES sweets (id) ON DELETE CASCADE ON UPDATE CASCADE), query
was: INSERT INTO sweets_results (customer_id, created_time,
update_time) VALUES (?, '2018-11-02 05:35:24', '2018-11-02
05:35:24')

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

MySQL cannot INSERT with FK constraint

I get the following error message when I try to INSERT some data:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(database/UserDetails, CONSTRAINT UserDetails_ibfk_6 FOREIGN
KEY (HearAboutID) REFERENCES UserDetails (HearAboutID) ON DELETE
CASCADE ON UPDATE CASCADE)
The FK relationship looks like this:
The data I'm trying to enter into UserDetails.HearAboutID is present in UserHearAbout.UserHearAboutID... so why won't it go? :(
From the error you posted
FOREIGN KEY (HearAboutID) REFERENCES UserDetails (HearAboutID) ON DELETE CASCADE ON UPDATE CASCADE)
(you actually created a constraint that refers to the same table and same column),
I guess that your FK constraint definition is wrong (it should be
FOREIGN KEY (HearAboutID) REFERENCES UserHearAbout (UserHearAboutID)
ON DELETE CASCADE ON UPDATE CASCADE)

Categories