David Garcia
David Garcia

Reputation: 2696

Oracle parents key not found

Im trying to add a constraint to a reservation table, the reservation can either be for a flight or accommodation or both.

Reservation

Here are my constraints for this table

ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT HOLIDAY_PK PRIMARY KEY (RESV_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT CUSTOMER_FK FOREIGN KEY (BOOKING_CUS_ID) REFERENCES CUSTOMER (CUS_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT STAFF_FK3 FOREIGN KEY (EMP_ID) REFERENCES STAFF (EMP_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT FLIGHT_FK FOREIGN KEY (IN_FLIGHT_ID) REFERENCES FLIGHT (FLI_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT FLIGHT_FK1 FOREIGN KEY (OUT_FLIGHT_ID) REFERENCES FLIGHT (FLI_ID);
ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT ACC_FK FOREIGN KEY (ACC_ID) REFERENCES ACCOMMODATION (ACC_ID);

and the only constraint that is yielding an error is;

ALTER TABLE HOLIDAY_RESERVATION ADD CONSTRAINT FLIGHT_FK1 FOREIGN KEY (OUT_FLIGHT_ID) REFERENCES FLIGHT (FLI_ID);

I get

ERROR at line 1:
ORA-02298: cannot validate (U1146815.FLIGHT_FK1) - parent keys not found

What seems to be the problem? i understand that it has to do with orphan childs, but i am setting nulls so i dont understand, please advise

Upvotes: 5

Views: 31051

Answers (2)

Mohammad Shahnawaz
Mohammad Shahnawaz

Reputation: 31

Delete all the rows from the child table which is being used to alter the column for having the references.

ALTER TABLE sales ADD CONSTRAINT sales_time_fk
FOREIGN KEY (time_id) REFERENCES times (time_id) 
RELY DISABLE NOVALIDATE;

Upvotes: 3

Justin Cave
Justin Cave

Reputation: 231661

The error indicates that the FLIGHT table does not have an entry for at least one of the FLI_ID values between 11 and 18. You'd need to insert a row in the FLIGHT table for whatever flights are missing or update your table to have a different OUT_FLIGHT_ID.

Upvotes: 16

Related Questions