sql - PostgreSQL constraint problems -
i having these 2 errors :
error: there no unique constraint matching given keys referenced table "flight"
*** error ***
error: there no unique constraint matching given keys referenced table "flight"
this code :
create table staff ( employeenumber int not null, firstname char(15) not null, lastname char(15) not null, socialsecurity int not null, sex char(1) not null, address char(20) not null, city char(20) not null, province char(15) not null, country char(20) not null, primary key (employeenumber) ); create table flightattendent ( faln int, staffrole char (20) not null, employeenumber int not null, foreign key (employeenumber) references staff(employeenumber), primary key (faln) ); create table aircrafttype ( actype char (10), instrument char(1) not null, engines int not null, crewcount int not null, passengercount int not null, primary key (actype) ); create table pilot ( piln int, medicalvalid date not null, staffrole char (20) not null, employeenumber int not null, aircrafttype char (10) not null, foreign key (employeenumber) references staff(employeenumber), foreign key (aircrafttype) references aircrafttype(actype), primary key (piln) ); create table aircraft ( aircraftid char(6) not null, aircraftmanufacturer char(10) not null, aircrafttype char(10) not null, foreign key (aircrafttype) references aircrafttype(actype), primary key (aircraftid) ); create table airport ( airportcode char(4) not null, airportname char(40) not null, city char(20) not null, country char(20) not null, continent char(20) not null, primary key (airportcode) ); create table flight ( flightid char (20), flightdate date, aircraftid char(6) not null, arrivalairport char(4) not null, departureairport char(4) not null, primary key (flightid, flightdate), foreign key (arrivalairport) references airport(airportcode), foreign key (departureairport) references airport(airportcode), foreign key (aircraftid) references aircraft(aircraftid) ); create table flightcrew ( flightid char (20) references flight(flightid) on delete cascade, flightdate date references flight(flightdate) on delete cascade, employeenumber int not null, staffrole char(20) not null, primary key(flightid, flightdate), foreign key (employeenumber) references staff(employeenumber) ); create table passenger ( passengernumber int, passportnumber int not null, citizenship char (20) not null, firstname char (20) not null, lastname char (20) not null, primary key (passengernumber) ); create table passengermanifest ( flightid char(20), flightdate date, passengernumber int not null, foreign key (flightdate) references flight(flightdate), foreign key (passengernumber) references passenger(passengernumber), primary key (flightid, flightdate) ); what did wrong? thanks!
when have multiple values in primary key, need reference differently foreign key.
basically, when say
flightid char (20) references flight(flightid) on delete cascade, postgresql checks primary key, doesn't exist (since primary key on table (flightid, flightdate)).
so drop references clauses when referencing flight table, , add
foreign key (flightid, flightdate) references flight (flightid, flightdate) in manner have in of other table definitions.
Comments
Post a Comment