Thursday, 8 August 2013

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

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

I'm trying to use an SQL script to automatically add a schedule to and
from every airport. Here is the code I have so far:
INSERT INTO phpvms_schedules (depicao, arricao, distance)
SELECT
depart.icao,
arrive.icao,
ROUND(GreaterCircleNm(depart.lat,depart.lng,arrive.lat,arrive.lng),2)
as Nm
FROM phpvms_airports AS depart
INNER JOIN phpvms_airports AS arrive ON depart.icao <> arrive.icao
GreaterCircleNm function:
DROP FUNCTION IF EXISTS GreaterCircleNm;
DELIMITER go
CREATE FUNCTION GreaterCircleNm( lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2
FLOAT ) RETURNS float
BEGIN
DECLARE pi, q1, dist FLOAT;
SET pi = PI();
SET lat1 = lat1 * pi / 180;
SET lon1 = lon1 * pi / 180;
SET lat2 = lat2 * pi / 180;
SET lon2 = lon2 * pi / 180;
SET q1 = ACOS(sin(lat1)*sin(lat2)+COS(lat1)*COS(lat2)*COS(lon1-lon2));
SET dist = q1*180*60/pi;
RETURN dist;
END;
go
DELIMITER ;
All in all I really need help fixing this error:
1452 - Cannot add or update a child row: a foreign key constraint fails
(virtu259_phpvms.phpvms_schedules, CONSTRAINT phpvms_schedules_ibfk_1
FOREIGN KEY (code) REFERENCES phpvms_airlines (code) ON UPDATE CASCADE)
phpvms_airports:

phpvms_schedules:

No comments:

Post a Comment