PostgreSQL 17.12Lab-verified · 2026-12-01

PostgreSQL error 23503: violates foreign key constraint

A row points to a parent key that does not exist, or a parent row is still referenced by child rows.

PostgreSQL 17.12
23503: insert or update on table "orders" violates foreign key constraint "orders_customer_id_fkey"

Jump to a cause

  1. 01Inserting a child row whose parent does not exist
  2. 02Deleting a parent row that child rows still reference
  3. 03Adding a foreign key when existing rows have no parent
  4. 04Changing a parent key (product code) that child rows still use
  5. 05Two tables that reference each other (department manager ↔ employee department)
  6. 06Imported code values that differ only in case (jp vs JP)

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17

23503 means a foreign key check failed. The first line tells you the direction: insert or update on table ... means the child row points to a missing parent; update or delete on table ... means you are removing a parent that child rows still use.

The DETAIL line gives the exact key value, such as Key (customer_id)=(3) is not present in table "customers". Start from that value.

01Inserting a child row whose parent does not exist

orders.customer_id references customers(id). Inserting an order for customer 3 fails, and DETAIL says Key (customer_id)=(3) is not present in table "customers".

The check confirms customer 3 does not exist (0 rows). After inserting the customer first, the same order insert succeeds. When loading data, insert parent tables before child tables.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, name text);
CREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers(id), amount numeric);
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
INSERT INTO orders VALUES (10, 3, 15000);
✕ ERROR
ERROR:  23503: insert or update on table "orders" violates foreign key constraint "orders_customer_id_fkey"
DETAIL:  Key (customer_id)=(3) is not present in table "customers".
SCHEMA NAME:  lab_a
TABLE NAME:  orders
CONSTRAINT NAME:  orders_customer_id_fkey
Checkrun as lab_a
SELECT id, name FROM customers WHERE id = 3;
OUTPUT
 id | name
----+------
(0 rows)
Fixrun as lab_a
INSERT INTO customers VALUES (3, 'Park');
OUTPUT
INSERT 0 1
After the fixrun as lab_a
INSERT INTO orders VALUES (10, 3, 15000) RETURNING id, customer_id, amount;
✓ SUCCESS
 id | customer_id | amount
----+-------------+--------
 10 |           3 |  15000
(1 row)

INSERT 0 1

02Deleting a parent row that child rows still reference

Deleting customer 1 fails with update or delete on table "customers" violates foreign key constraint ... on table "orders", and DETAIL says Key (id)=(1) is still referenced from table "orders".

The check finds two orders (10, 11) for customer 1. After deleting those orders (DELETE 2), deleting the customer works (DELETE 1). Make sure the child rows really can be removed before doing this.

Setup
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, name text);
CREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers(id), amount numeric);
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');
INSERT INTO orders VALUES (10, 1, 15000), (11, 1, 8000);
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 2
Reproducerun as lab_a
DELETE FROM customers WHERE id = 1;
✕ ERROR
ERROR:  23503: update or delete on table "customers" violates foreign key constraint "orders_customer_id_fkey" on table "orders"
DETAIL:  Key (id)=(1) is still referenced from table "orders".
SCHEMA NAME:  lab_a
TABLE NAME:  orders
CONSTRAINT NAME:  orders_customer_id_fkey
Checkrun as lab_a
SELECT id, customer_id, amount FROM orders WHERE customer_id = 1 ORDER BY id;
OUTPUT
 id | customer_id | amount
----+-------------+--------
 10 |           1 |  15000
 11 |           1 |   8000
(2 rows)
Fixrun as lab_a
DELETE FROM orders WHERE customer_id = 1;
OUTPUT
DELETE 2
After the fixrun as lab_a
DELETE FROM customers WHERE id = 1;
✓ SUCCESS
DELETE 1

03Adding a foreign key when existing rows have no parent

Adding a foreign key to a table that already has data checks every row. Employee 3 has dept_id 30, which is not in departments, so ALTER TABLE ... ADD CONSTRAINT fails with Key (dept_id)=(30) is not present. The message still says insert or update even though you ran ALTER TABLE.

A LEFT JOIN finds the orphan row (Choi, 30). After setting its dept_id to NULL (NULL is not checked by a foreign key), the constraint is added.

Setup
Setuprun as lab_a
CREATE TABLE departments (id int PRIMARY KEY, name text);
CREATE TABLE employees (id int PRIMARY KEY, name text, dept_id int);
INSERT INTO departments VALUES (10, 'Sales'), (20, 'IT');
INSERT INTO employees VALUES (1, 'Kim', 10), (2, 'Lee', 20), (3, 'Choi', 30);
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 3
Reproducerun as lab_a
ALTER TABLE employees ADD CONSTRAINT employees_dept_fk FOREIGN KEY (dept_id) REFERENCES departments(id);
✕ ERROR
ERROR:  23503: insert or update on table "employees" violates foreign key constraint "employees_dept_fk"
DETAIL:  Key (dept_id)=(30) is not present in table "departments".
SCHEMA NAME:  lab_a
TABLE NAME:  employees
CONSTRAINT NAME:  employees_dept_fk
Checkrun as lab_a
SELECT e.id, e.name, e.dept_id FROM employees e LEFT JOIN departments d ON d.id = e.dept_id WHERE e.dept_id IS NOT NULL AND d.id IS NULL;
OUTPUT
 id | name | dept_id
----+------+---------
  3 | Choi |      30
(1 row)
Fixrun as lab_a
UPDATE employees SET dept_id = NULL WHERE dept_id = 30;
OUTPUT
UPDATE 1
After the fixrun as lab_a
ALTER TABLE employees ADD CONSTRAINT employees_dept_fk FOREIGN KEY (dept_id) REFERENCES departments(id);
✓ SUCCESS
ALTER TABLE

04Changing a parent key (product code) that child rows still use

Renaming product code A-100 to KB-100 fails with update or delete on table "products" violates foreign key constraint. It is an UPDATE, not a DELETE, but the message is the same, and DETAIL says Key (code)=(A-100) is still referenced from table "order_items".

The constraint was recreated with ON UPDATE CASCADE. Now the same UPDATE succeeds (UPDATE 1) and both order lines change to KB-100 together.

Setup
Setuprun as lab_a
CREATE TABLE products (code text PRIMARY KEY, name text);
CREATE TABLE order_items (id int PRIMARY KEY, product_code text REFERENCES products(code), qty int);
INSERT INTO products VALUES ('A-100', 'Keyboard');
INSERT INTO order_items VALUES (1, 'A-100', 2), (2, 'A-100', 1);
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 1
INSERT 0 2
Reproducerun as lab_a
UPDATE products SET code = 'KB-100' WHERE code = 'A-100';
✕ ERROR
ERROR:  23503: update or delete on table "products" violates foreign key constraint "order_items_product_code_fkey" on table "order_items"
DETAIL:  Key (code)=(A-100) is still referenced from table "order_items".
SCHEMA NAME:  lab_a
TABLE NAME:  order_items
CONSTRAINT NAME:  order_items_product_code_fkey
Check the causerun as lab_a
SELECT conname, confupdtype, confdeltype FROM pg_constraint WHERE conrelid = 'order_items'::regclass AND contype = 'f';
OUTPUT
            conname            | confupdtype | confdeltype
-------------------------------+-------------+-------------
 order_items_product_code_fkey | a           | a
(1 row)

confupdtype is a (NO ACTION): changing the parent key is blocked while child rows use it.

Fixrun as lab_a
ALTER TABLE order_items DROP CONSTRAINT order_items_product_code_fkey;
ALTER TABLE order_items ADD CONSTRAINT order_items_product_code_fkey FOREIGN KEY (product_code) REFERENCES products(code) ON UPDATE CASCADE;
OUTPUT
ALTER TABLE
ALTER TABLE
After the fixrun as lab_a
UPDATE products SET code = 'KB-100' WHERE code = 'A-100';
SELECT id, product_code, qty FROM order_items ORDER BY id;
✓ SUCCESS
UPDATE 1
 id | product_code | qty
----+--------------+-----
  1 | KB-100       |   2
  2 | KB-100       |   1
(2 rows)

05Two tables that reference each other (department manager ↔ employee department)

departments.manager_id points to employees, and employees.dept_id points to departments. Whichever row you insert first, its parent does not exist yet. The first INSERT fails with Key (manager_id)=(1) is not present in table "employees", the rest of the transaction is skipped (25P02: current transaction is aborted), and COMMIT ends as ROLLBACK.

After making the manager constraint DEFERRABLE INITIALLY DEFERRED, it is checked at COMMIT instead of per statement. The same transaction commits and the department shows its manager Kim.

Setup
Setuprun as lab_a
CREATE TABLE departments (id int PRIMARY KEY, name text, manager_id int NOT NULL);
CREATE TABLE employees (id int PRIMARY KEY, name text, dept_id int NOT NULL REFERENCES departments(id));
ALTER TABLE departments ADD CONSTRAINT departments_manager_fk FOREIGN KEY (manager_id) REFERENCES employees(id);
OUTPUT
CREATE TABLE
CREATE TABLE
ALTER TABLE
Reproducerun as lab_a
BEGIN;
INSERT INTO departments VALUES (10, 'Sales', 1);
INSERT INTO employees VALUES (1, 'Kim', 10);
COMMIT;
✕ ERROR
BEGIN
ERROR:  23503: insert or update on table "departments" violates foreign key constraint "departments_manager_fk"
DETAIL:  Key (manager_id)=(1) is not present in table "employees".
SCHEMA NAME:  lab_a
TABLE NAME:  departments
CONSTRAINT NAME:  departments_manager_fk
ERROR:  25P02: current transaction is aborted, commands ignored until end of transaction block
ROLLBACK
Check the causerun as lab_a
SELECT conname, conrelid::regclass AS table_name, condeferrable, condeferred FROM pg_constraint WHERE contype = 'f' AND conrelid IN ('departments'::regclass, 'employees'::regclass) ORDER BY conname;
OUTPUT
        conname         | table_name  | condeferrable | condeferred
------------------------+-------------+---------------+-------------
 departments_manager_fk | departments | f             | f
 employees_dept_id_fkey | employees   | f             | f
(2 rows)

Both foreign keys have condeferrable = f: each INSERT is checked immediately, so neither order can work.

Fixrun as lab_a
ALTER TABLE departments ALTER CONSTRAINT departments_manager_fk DEFERRABLE INITIALLY DEFERRED;
OUTPUT
ALTER TABLE
After the fixrun as lab_a
BEGIN;
INSERT INTO departments VALUES (10, 'Sales', 1);
INSERT INTO employees VALUES (1, 'Kim', 10);
COMMIT;
SELECT d.id, d.name, e.name AS manager FROM departments d JOIN employees e ON e.id = d.manager_id;
✓ SUCCESS
BEGIN
INSERT 0 1
INSERT 0 1
COMMIT
 id | name  | manager
----+-------+---------
 10 | Sales | Kim
(1 row)

06Imported code values that differ only in case (jp vs JP)

Foreign keys compare values exactly. Copying rows from an import table (e.g. loaded from CSV) fails at the first bad row with Key (country_code)=(jp) is not present in table "countries", and no rows are inserted, because countries has JP and KR.

After converting the import data with upper() (UPDATE 2), the same INSERT ... SELECT inserts all 3 rows (INSERT 0 3).

Setup
Setuprun as lab_a
CREATE TABLE countries (code char(2) PRIMARY KEY, name text);
CREATE TABLE customers (id int PRIMARY KEY, name text, country_code char(2) REFERENCES countries(code));
INSERT INTO countries VALUES ('KR', 'Korea'), ('JP', 'Japan');
CREATE TABLE import_customers (id int, name text, country_code char(2));
INSERT INTO import_customers VALUES (1, 'Kim', 'KR'), (2, 'Sato', 'jp'), (3, 'Lee', 'kr');
OUTPUT
CREATE TABLE
CREATE TABLE
INSERT 0 2
CREATE TABLE
INSERT 0 3
Reproducerun as lab_a
INSERT INTO customers SELECT id, name, country_code FROM import_customers;
✕ ERROR
ERROR:  23503: insert or update on table "customers" violates foreign key constraint "customers_country_code_fkey"
DETAIL:  Key (country_code)=(jp) is not present in table "countries".
SCHEMA NAME:  lab_a
TABLE NAME:  customers
CONSTRAINT NAME:  customers_country_code_fkey
Check the causerun as lab_a
SELECT i.id, i.name, i.country_code, c.code AS parent_code
FROM import_customers i LEFT JOIN countries c ON c.code = i.country_code
WHERE c.code IS NULL ORDER BY i.id;
OUTPUT
 id | name | country_code | parent_code
----+------+--------------+-------------
  2 | Sato | jp           |
  3 | Lee  | kr           |
(2 rows)

The LEFT JOIN finds 2 rows with no parent (jp, kr). Only the case differs from JP and KR.

Fixrun as lab_a
UPDATE import_customers SET country_code = upper(country_code) WHERE country_code <> upper(country_code);
OUTPUT
UPDATE 2
After the fixrun as lab_a
INSERT INTO customers SELECT id, name, country_code FROM import_customers;
SELECT id, name, country_code FROM customers ORDER BY id;
✓ SUCCESS
INSERT 0 3
 id | name | country_code
----+------+--------------
  1 | Kim  | KR
  2 | Sato | JP
  3 | Lee  | KR
(3 rows)

FAQ

How do I fix insert or update on table violates foreign key constraint?

Read the DETAIL line for the missing key, then insert that parent row first or correct the child's key value. When loading data, load parent tables before child tables.

How do I delete a row that is still referenced from another table?

Find the child rows from the table named in DETAIL, and delete or reassign them first. Then delete the parent.

Why does ALTER TABLE ADD FOREIGN KEY fail with 23503?

Existing rows already contain values that are not in the parent table. Find them with a LEFT JOIN ... WHERE parent.id IS NULL, fix or clear them, then add the constraint again.

How do I insert into two tables that reference each other?

Make one of the foreign keys DEFERRABLE INITIALLY DEFERRED. It is then checked at COMMIT, so both rows can be inserted in one transaction.

Why does UPDATE of a primary key fail with is still referenced from table?

Child rows still use the old key and the constraint is NO ACTION on update. Recreate the foreign key with ON UPDATE CASCADE so children follow the new key.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12