F
F
fachel322019-12-22 21:29:27
PostgreSQL
fachel32, 2019-12-22 21:29:27

Why is there an error "There is no unique constraint corresponding to the given keys in the target external table 'apartment'"?

there are 3 tables :
CREATE TABLE BUILDING(
housing_name CHARACTER VARYING(20) PRIMARY KEY
);
CREATE TABLE APARTMENT( --NUMBER
number_of_apartment INTEGER ,
housing_name CHARACTER VARYING(30) REFERENCES BUILDING (housing_name) ,
PRIMARY KEY(housing_name, number_of_apartment)
);
CREATE TABLE ACCOMMODATION( --RESIDENCE
id serial primary key,
number_of_apartment INTEGER ,
housing_name CHARACTER VARYING(30) ,
FOREIGN KEY (housing_name, number_of_apartment ) REFERENCES APARTMENT (housing_name,
number_of_apartment)
);
Tables 1 and 2 are created without errors, when creating the 3rd one an error occurs:
"the target foreign table 'apartment' does not have a unique constraint corresponding to the given keys"
Although in 'apartment' housing_name and number_of_apartment are a composite key.
How to solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-12-22
@fachel32

The pair of fields that you are trying to use as a foreign key may not have unique values, as a result it will not be clear which row the foreign key refers to.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question