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.
Related
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.
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')
I want to insert into a table called Festivals that has two foreign keys (venueID & ticketID).
The SQL:
$addFestivalSql = "INSERT INTO festivals
VALUES(DEFAULT, :festivalName, :festivalDescription,
:festivalImage, :festivalLineup,
:musicTypeId, DEFAULT, DEFAULT)";
The error:
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
The Festivals Table:
The Festivals Table
I've created the two foreign key constraints with venueID and ticketID relating to the two primary keys.
Why do you think I'm getting this error?
Thanks.
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)
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