INSERT INTO... SELECT in same table generates foreign key error - mysql

I am having trouble with the following insert query.
INSERT INTO CM_LABEL_CALENDAR (
label_id,
label_name,
order_seq,
meal_id,
hyperlink
)
SELECT
label_id,
label_name,
order_seq,
(meal_id + 315),
hyperlink
FROM
CM_LABEL_CALENDAR
WHERE
(meal_id BETWEEN '1466' AND '1521');
When I try to execute it I get the following error:
Lookup Error - MySQL Database Error: Cannot add or update a child row: a foreign key constraint fails (TEST_PBMS.CM_LABEL_CALENDAR, CONSTRAINT CM_LABEL_CALENDAR_ibfk_1 FOREIGN KEY (meal_id) REFERENCES CM_MEAL_CALENDAR (meal_id))
I've tried looking for an answer but couldn't find one.

There is a foreign key constraint between CM_LABEL_CALENDAR(meal_id) and CM_MEAL_CALENDAR(meal_id)
You are getting this error because you are trying to insert values in the meal_id column that do not exist in the CM_MEAL_CALENDAR table.

Related

how to make a one to one relationship in phpmyadmin?

I have two tables "donor" and " location", every donor has one location in a time.
how to make the keys for this relation?
I tried to make a foreign key in location table to the donor but it gives me this message:
Error
SQL query:
ALTER TABLE `location` ADD CONSTRAINT `location_donor` FOREIGN KEY (`donor_id`) REFERENCES `blood_donation`.`donor`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;
MySQL said: Documentation
#1452 - Cannot add or update a child row: a foreign key constraint fails (`blood_donation`.`#sql-23f8_2e`, CONSTRAINT `location_donor` FOREIGN KEY (`donor_id`) REFERENCES `donor` (`id`))
If you want to execute ALTER TABLE statment you should truncate the table in the first place.
Because Mysql can not add the constraint to the existing rows according to the error log :
#1452 - Cannot add or update a child row: a foreign key constraint fails

Sql query not running

When I run the following query on phpmyadmin then it gives me following error
INSERT INTO `infrastructure_support_info`(`s_no`, `student_no`, `books_availability`, `basic_requirements`, `technological_support`, `study_material`, `resource_availability`, `cleaniliness_of_class`)
VALUES ('', '', '$availabilityOfBooks', '$basicRequirements', '$technologicalSupport', '$photocopyOfStudyMaterial', '$availabilityOfOtherResources', '$cleanlinessOfClass')
#1452 - Cannot add or update a child row: a foreign key constraint fails (feedback_system_db.infrastructure_support_info, CONSTRAINT user_std_no FOREIGN KEY (student_no) REFERENCES user_master (student_no))
Your table has a reference to another one. If you want to leave that link empty then use null instead of a blank string ''.

Error when adding foreign key

I had a table named movies which had the fields id as primary key, and two varchars: title and genre.
I created a new table named genres with the int field id as primary key and desription varchar. I changed the field genre in my movies table so I could create a foreign key referencing a genre.
However, Mysql Workbench says there's an error when creating the foreign key.
Here's the statement:
ALTER TABLE `managemovies`.`movies`
ADD CONSTRAINT `genre_reference`
FOREIGN KEY (`genre` )
REFERENCES `managemovies`.`genres` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
, ADD INDEX `genre_reference_idx` (`genero` ASC) ;
Error:
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (`managemovies`.`#sql-3ba_2b`, CONSTRAINT `genre_reference` FOREIGN KEY (`genre`) REFERENCES `genres` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement: [... same statement than above ... ]
ERROR: Error when running failback script. Details follow.
ERROR 1046: No database selected
SQL Statement:
CREATE TABLE `movies` [...]
[... the errors above repeated again ...]
clear your table contents and try adding foreign key.
if your table contain data which not matching the foreign key field value you will see this error ...
It looks like your table movies has data in genre column which is not present in genres.id column.
Your statement should work after removing the invalid data.
Hope it helps
Vishad
I have faced same issue i got resolved later..
for this answer is simple you just need to add atleast a row of values in both tables then try to add the foreign key

Cannot add or update a child row: a foreign key constraint fails webERp

I use WebERP in my 1and1 account, when I migrate my database to another 1and1 database I get this error:
SQL query:
--
-- Constraints for table `chartdetails`
--
ALTER TABLE `chartdetails` ADD CONSTRAINT `chartdetails_ibfk_1` FOREIGN KEY ( `accountcode` )
REFERENCES `chartmaster` ( `accountcode` ) ,
ADD CONSTRAINT `chartdetails_ibfk_2` FOREIGN KEY ( `period` ) REFERENCES `periods` ( `periodno` )
MySQL said:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`dbxxxxxxxxx/#sql- 376_3fa4f12`, CONSTRAINT `chartdetails_ibfk_2` FOREIGN KEY (`period`) REFERENCES `periods` (`periodno`))
But the original file just work fine.
I got the same Error when i migrate. I resolved this error in 3 ways. You can resolve your error on any one of them or an all. This happens because of no data insret query happens before you alter.
• Put Alter table queries on last of all other queries.
• Twice check for the presence of data where the primary key is present
• Install fresh DB later insert in hierarchical order like chart master first and later chart detail insert queries.
Note: DB wont allow you to delete or insert when you try to change db queries. Keep backups of DB before making any changes.

error adding a foreign key constraint

I have the following query:
ALTER TABLE ROUTE ADD FOREIGN KEY (RID) REFERENCES RESERVATION(RID) ON DELETE CASCADE
but it generates me an error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`SmarTrek`.`#sql-91e_d09`, CONSTRAINT `FK_RID` FOREIGN KEY (`RID`) REFERENCES `RESERVATION` (`RID`) ON DELETE CASCADE)
In designer mode, here's what it looks like:
That would meant that you already have data in the ROUTE table that does not satisfy the foreign key constraint.
To find the offending records, so you can update them to some other value (that exists), you can use
select *
from route
where rid not in (select rid from reservation)
THERE may b 2 reasons
there may b some row in ROUTE TABLE which have RID that does not exist in RESERVATION(RID)
or check the DATATYPE OF ROUTE (RID) & RESERVATION(RID) both should be same ( unsigned/signed)