MySQL cannot INSERT with FK constraint - php

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)

Related

How to bypass/disable Foreign Key Constraints on Laravel Queries

Laminas\Db\Adapter\Exception\InvalidQueryException
Statement could not be executed (23000 - 1452 - Cannot add or update a child row: a foreign key constraint fails (lms.student_course_logs, CONSTRAINT student_course_logs_lecture_id_foreign FOREIGN KEY (lecture_id) REFERENCES courses (id) ON DELETE CASCADE))
http://localhost/public/student/course/loglecture
I'm having this foreign key constraints in my query. How do I solve this one? Would it be possible to bypass foreign key contraints?
Here's my error log: https://flareapp.io/share/omwaaZR7#F51
Thank you.

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.

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')

error while inserting in codeigniter

I am getting this error while inserting in my table:
A Database Error Occurred
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails (`giftme`.`giftme_shop`, CONSTRAINT `shop_ibfk_2` FOREIGN KEY (`sh_cid`) REFERENCES `giftme_shop_category` (`sc_cid`) ON DELETE CASCADE)
INSERT INTO `giftme_shop` (`sh_uid`, `sh_shop_name`, `sh_cid`) VALUES ('79', 'Naveen Reddys shop', 1)
Filename: /var/www/apps/helpers/sessions_helper.php
Line Number: 55
Any solution please?
there is foreign key reference
parent : `giftme_shop_category`.`sc_cid`
child : `giftme_shop`.sh_cid
there is no value of sc_cid=1 in parent table
Remove Foreign Key constraint
ALTER TABLE giftme_shop DROP FOREIGN KEY shop_ibfk_2;
read here
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol

Categories