Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • First 4 records booked inward flight, outward flight and accommodation
  • Next 4 records booked a flight only and have acc_id set to NULL
  • Following 2 records booked only accommodation, hence in flight, out flight and seats are set to null.

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

like image 860
David Garcia Avatar asked Dec 03 '22 04:12

David Garcia


2 Answers

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.

like image 200
Justin Cave Avatar answered Dec 29 '22 14:12

Justin Cave


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;
like image 42
Mohammad Shahnawaz Avatar answered Dec 29 '22 12:12

Mohammad Shahnawaz