PostgreSQL 17.12実機検証済み · 2026-11-29

PostgreSQL 42830: 被参照テーブルに、指定したキーに一致する一意性制約がありません

外部キーが参照する親の列に PRIMARY KEY も UNIQUE 制約もありません。

PostgreSQL 17.12
42830: 被参照テーブル"customers"に、指定したキーに一致する一意性制約がありません
42830: there is no unique constraint matching given keys for referenced table "customers"

原因へ移動

  1. 01親テーブルに PRIMARY KEY がない
  2. 02UNIQUE でない列(email)を参照
  3. 03複合 PK の1列だけを参照

実機検証済み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 なのに1列だけ参照した、の3つです。

どれも制約の側で解決しました。親に足りないキーを追加するか、外部キーを親の既存キーに合わせています。

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 は成功しました。

事前準備
事前準備lab_a で実行
CREATE TABLE customers (id int, email text NOT NULL);
OUTPUT
CREATE TABLE
再現lab_a で実行
CREATE 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"
確認lab_a で実行
SELECT conname, contype FROM pg_constraint WHERE conrelid = 'customers'::regclass;
OUTPUT
 conname | contype
---------+---------
(0 rows)
対処lab_a で実行
ALTER TABLE customers ADD PRIMARY KEY (id);
OUTPUT
ALTER TABLE
対処後の再実行lab_a で実行
CREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers (id));
✓ 成功
CREATE TABLE

02UNIQUE でない列(email)を参照

customers の PK は id ですが、新しいテーブルは customers (email) を参照しました。email に UNIQUE がないため 42830 になりました。

重複 email の確認が 0 件だったので ADD CONSTRAINT customers_email_key UNIQUE (email) を追加し、CREATE TABLE は成功しました。

事前準備
事前準備lab_a で実行
CREATE TABLE customers (id int PRIMARY KEY, email text NOT NULL);
OUTPUT
CREATE TABLE
再現lab_a で実行
CREATE 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"
確認lab_a で実行
SELECT email, count(*) FROM customers GROUP BY email HAVING count(*) > 1;
OUTPUT
 email | count
-------+-------
(0 rows)
対処lab_a で実行
ALTER TABLE customers ADD CONSTRAINT customers_email_key UNIQUE (email);
OUTPUT
ALTER TABLE

重複確認で行が返る場合は、先に重複を整理しないと UNIQUE を追加できません。

対処後の再実行lab_a で実行
CREATE TABLE newsletters (id int PRIMARY KEY, customer_email text REFERENCES customers (email));
✓ 成功
CREATE TABLE

03複合 PK の1列だけを参照

order_items の PK は PRIMARY KEY (order_id, line_no) です。REFERENCES order_items (order_id) は2列のうち1列だけで、それだけでは一意ではないため 42830 になりました。

pg_get_constraintdef でキーを確認し、子に line_no を足して FOREIGN KEY (order_id, line_no) REFERENCES order_items (order_id, line_no) にすると成功しました。

事前準備
事前準備lab_a で実行
CREATE TABLE order_items (order_id int, line_no int, product text, PRIMARY KEY (order_id, line_no));
OUTPUT
CREATE TABLE
再現lab_a で実行
CREATE 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"
確認lab_a で実行
SELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'order_items'::regclass;
OUTPUT
     conname      |      pg_get_constraintdef
------------------+---------------------------------
 order_items_pkey | PRIMARY KEY (order_id, line_no)
(1 row)
対処後の再実行lab_a で実行
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 を付けるか、外部キーを親の既存キー列と同じにします。

参照先は主キーでないとだめですか?

いいえ。2つ目の例のように、主キーでない email に UNIQUE を付けるだけで成功しました。

親が複合主キーの場合はどう参照しますか?

キー列をすべてまとめて参照します。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