SQL error · INSERT, UPDATE, DELETE and schema

FOREIGN KEY blocks an insert or delete

Problem: The child reference has no parent, or you are trying to delete a parent that is still referenced.

Solution: Check the key and the load order; before deleting, look at the child rows and the action defined on the key.

Why it happens: FOREIGN KEY blocks an insert or delete

Schema and failing operation
CREATE TABLE customers (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL,
  FOREIGN KEY (customer_id) REFERENCES customers(id)
);

INSERT INTO customers (id, name)
VALUES (1, 'Amy');

INSERT INTO orders (id, customer_id)
VALUES (101, 99);

The order tries to store customer_id = 99, but the table customers does not contain that key. The fix is not to drop the constraint: first decide whether the customer is missing, the identifier is wrong or the relationship should be optional.

“FOREIGN KEY blocks an insert or delete” error messages by engine

The exact wording depends on the database. Compare these common messages with the one in your console.

EngineTypical message
PostgreSQLERROR: insert or update on table "orders" violates foreign key constraint "orders_customer_id_fkey"
MySQLERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (al delete un parent: ERROR 1451)
SQL ServerMsg 547, Level 16: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_orders_customers".
OracleORA-02291: integrity constraint (ESQUEMA.FK_PEDIDOS_CLIENTES) violated - parent key not found (al delete un parent: ORA-02292)
SQLiteRuntime error: FOREIGN KEY constraint failed (with PRAGMA foreign_keys = ON)

How to fix it in SQL

Fix the data or the order of the operations

Valid insert
INSERT INTO customers (id, name)
VALUES (99, 'Liam');

INSERT INTO orders (id, customer_id)
VALUES (101, 99);
Valid relationship after creating the parent row first
order_idcustomer_idcustomer
10199Liam

If DELETE fails, look for the child rows first

Before deleting a parent row, check which tables reference it. With the usual default action, the database will not let child rows point to a key that no longer exists. Actions such as CASCADE, SET NULL or SET DEFAULT change that behaviour and should be chosen because they fit the data model, not to dodge a one-off error.

Do not disable foreign keys as your first fix. You may turn a visible error into inconsistent data that is much harder to repair.

How to check it

Look up the key in the parent table and all its child references before trying the change again.

How to avoid it next time

Do not switch off integrity checks to accept a wrong reference.

Related cases worth checking

PostgreSQL enforces the foreign key constraints defined in the schema. SQLite also supports foreign keys, but its documentation states that the application must enable enforcement for each connection where it is not on by default. If you try the examples in SQLite, confirm that setting before concluding that an invalid insert “works”.