실제 DB 검증PostgreSQL 17.12에서 재현·해결 확인 · 2026-11-29 · 컨테이너 이미지 postgres:17
PostgreSQL 외래키는 부모 테이블에서 PRIMARY KEY나 UNIQUE 제약이 걸린 컬럼(조합까지 똑같이)만 참조할 수 있어요. 그런 제약이 없으면 CREATE TABLE ... REFERENCES나 ALTER TABLE ... ADD FOREIGN KEY가 42830으로 실패하고 자식 테이블도 만들어지지 않아요.
아래 3가지가 흔한 경우예요. 부모 테이블에 PK가 아예 없거나, email 같은 일반 컬럼을 참조했거나, 부모가 복합 PK인데 그중 한 컬럼만 참조했어요.
모두 제약 쪽에서 해결했어요. 부모에 빠진 키를 추가하거나, 외래키를 부모의 기존 키와 맞췄어요.
01부모 테이블에 PRIMARY KEY가 없음
customers를 PK 없이 (id int, email text NOT NULL)로 만들었더니 REFERENCES customers (id)가 42830으로 실패했어요.
pg_constraint에서 customers의 제약을 조회하니 0건이었어요. ALTER TABLE customers ADD PRIMARY KEY (id) 뒤에 같은 CREATE TABLE orders가 성공했어요.
사전 준비
CREATE TABLE customers (id int, email text NOT NULL);CREATE TABLECREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers (id));ERROR: 42830: there is no unique constraint matching given keys for referenced table "customers"SELECT conname, contype FROM pg_constraint WHERE conrelid = 'customers'::regclass; conname | contype
---------+---------
(0 rows)ALTER TABLE customers ADD PRIMARY KEY (id);ALTER TABLECREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers (id));CREATE TABLE02UNIQUE가 아닌 컬럼(email)을 참조
customers의 PK는 id인데 새 테이블이 customers (email)을 참조했어요. email에는 UNIQUE가 없어서 42830이 났어요.
중복 email 조회가 0건이라 ADD CONSTRAINT customers_email_key UNIQUE (email)을 추가했고, 그 뒤 CREATE TABLE이 성공했어요.
사전 준비
CREATE TABLE customers (id int PRIMARY KEY, email text NOT NULL);CREATE TABLECREATE TABLE newsletters (id int PRIMARY KEY, customer_email text REFERENCES customers (email));ERROR: 42830: there is no unique constraint matching given keys for referenced table "customers"SELECT email, count(*) FROM customers GROUP BY email HAVING count(*) > 1; email | count
-------+-------
(0 rows)ALTER TABLE customers ADD CONSTRAINT customers_email_key UNIQUE (email);ALTER TABLE중복 조회에서 행이 나오면 중복을 먼저 정리해야 UNIQUE를 추가할 수 있어요.
CREATE TABLE newsletters (id int PRIMARY KEY, customer_email text REFERENCES customers (email));CREATE TABLE03복합 PK 중 한 컬럼만 참조
order_items의 PK는 PRIMARY KEY (order_id, line_no)예요. REFERENCES order_items (order_id)는 두 컬럼 중 하나만 써서 그것만으로는 유일하지 않으니 42830이 났어요.
pg_get_constraintdef로 키가 PRIMARY KEY (order_id, line_no)인 걸 확인하고, 자식에 line_no를 추가해 FOREIGN KEY (order_id, line_no) REFERENCES order_items (order_id, line_no)로 만드니 성공했어요.
사전 준비
CREATE TABLE order_items (order_id int, line_no int, product text, PRIMARY KEY (order_id, line_no));CREATE TABLECREATE TABLE shipments (id int PRIMARY KEY, order_id int REFERENCES order_items (order_id));ERROR: 42830: there is no unique constraint matching given keys for referenced table "order_items"SELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'order_items'::regclass; conname | pg_get_constraintdef
------------------+---------------------------------
order_items_pkey | PRIMARY KEY (order_id, line_no)
(1 row)CREATE TABLE shipments (id int PRIMARY KEY, order_id int, line_no int, FOREIGN KEY (order_id, line_no) REFERENCES order_items (order_id, line_no));CREATE TABLE자주 묻는 질문
there is no unique constraint matching given keys 해결 방법은요?
참조하는 부모 컬럼에 PRIMARY KEY나 UNIQUE를 걸거나, 외래키를 부모의 기존 키 컬럼과 똑같이 맞춰요.
참조 컬럼이 꼭 PK여야 하나요?
아니에요. 두 번째 경우처럼 PK가 아닌 email에 UNIQUE만 걸어도 됐어요.
부모가 복합 PK면 어떻게 참조해요?
키 컬럼을 모두 함께 참조해요. FOREIGN KEY (order_id, line_no) REFERENCES order_items (order_id, line_no)처럼요.
부모 테이블에 어떤 키가 있는지 어떻게 봐요?
SELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'order_items'::regclass;로 확인해요.
검증 방법
시나리오마다 새 DB 사용자를 만들어 시작해요. 스크립트가 사전 준비 → 에러 재현(에러 코드 일치 확인) → 해결 → 원래 문장 재실행 순서로 실행하고, 화면의 출력은 DB가 실제로 낸 결과를 스크립트가 그대로 저장한 거예요.
lab_a, lab_b는 테스트용 일반 역할(같은 이름의 스키마 소유), postgres는 슈퍼유저예요. PostgreSQL 17.12