# ON DELETE RESTRICT vs NO ACTION: What Changed in PostgreSQL 18, and What MySQL and SQL Server Do

> Both refuse to delete a parent row that children still reference. `NO ACTION` checks once the statement, or a deferred transaction, is done, so a repair in between is allowed; `RESTRICT` refuses the moment the parent goes. Since PostgreSQL 18 they also fail with different SQLSTATEs, 23001 for `RESTRICT` and 23503 for `NO ACTION`, so code that recognises 23503, including Rails' `InvalidForeignKey`, stops catching `RESTRICT` refusals after the upgrade. MySQL treats the two as one, SQL Server has no `RESTRICT`, and MySQL accepts `SET DEFAULT` without ever doing it.

Source: https://schemity.com/blog/on-delete-restrict-vs-no-action/

`ON DELETE RESTRICT` and `ON DELETE NO ACTION` both refuse to delete a parent row that child rows still point at, and for a plain `DELETE` you cannot tell them apart. They differ in when the check runs: `NO ACTION` waits until the end of the statement, or until commit on a deferred key, while `RESTRICT` refuses the moment the parent row goes. Since PostgreSQL 18 they also fail with different error codes, which is the part that breaks working applications on upgrade.

Everything below ran in throwaway containers on PostgreSQL 17.11, 18.3 and 19.0, MySQL 8.4.11, MariaDB 11.4.13, SQL Server 2022 (CU26) and SQLite 3.54.0 with `PRAGMA foreign_keys = ON`, with one customer table and an invoice table per action. The keys are written as table constraints on purpose: MySQL 8.4 parses an inline `customer_id int REFERENCES customers (id)` and silently creates no foreign key at all, so on MySQL the delete below would simply succeed.

```sql
CREATE TABLE customers (id int PRIMARY KEY);
CREATE TABLE inv_restrict (id int PRIMARY KEY, customer_id int NOT NULL,
  FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE RESTRICT);
CREATE TABLE inv_noaction (id int PRIMARY KEY, customer_id int NOT NULL,
  FOREIGN KEY (customer_id) REFERENCES customers (id) ON DELETE NO ACTION);
INSERT INTO customers VALUES (1), (2);
INSERT INTO inv_restrict VALUES (1, 1);
INSERT INTO inv_noaction VALUES (1, 2);
DELETE FROM customers WHERE id = 1;
DELETE FROM customers WHERE id = 2;
```

## ON DELETE RESTRICT vs NO ACTION: what is the difference?

`NO ACTION` checks for orphans after the statement, or at commit when the key is deferred; `RESTRICT` checks the moment the parent row goes, and can never be deferred.

The [PostgreSQL manual](https://www.postgresql.org/docs/18/ddl-constraints.html) puts it in one line: `RESTRICT` "does not allow the check to be deferred until later in the transaction." The difference shows without any deferral, in one statement that deletes a parent and puts it back:

```sql
WITH d AS (DELETE FROM customers WHERE id = 2 RETURNING id)
INSERT INTO customers SELECT id FROM d;  -- NO ACTION: succeeds
WITH d AS (DELETE FROM customers WHERE id = 1 RETURNING id)
INSERT INTO customers SELECT id FROM d;  -- RESTRICT: refused at the DELETE
```

On PostgreSQL 18.3 the first statement ran and the second was refused, because `NO ACTION` looked at the table once the statement had finished and found the customer back. On a key declared `DEFERRABLE INITIALLY DEFERRED`, the same window stretches to the whole transaction for `NO ACTION`, and stays shut for `RESTRICT`. SQLite behaved the same way: a deferred `NO ACTION` key let one transaction delete a customer and insert it again, and a deferred `RESTRICT` key refused the delete. Whether a key should be deferrable at all is its own decision, covered in [PostgreSQL deferrable foreign keys](https://schemity.com/blog/postgres-deferrable-foreign-keys/).

## What changed for RESTRICT in PostgreSQL 18?

PostgreSQL 18 reports a refused `RESTRICT` with SQLSTATE 23001, `restrict_violation`, where 17 used 23503, `foreign_key_violation`, the code `NO ACTION` still uses.

The same `DELETE FROM customers WHERE id = 1` produced:

| | PostgreSQL 17.11 | PostgreSQL 18.3 and 19.0 |
|---|---|---|
| `RESTRICT` | `23503`, `violates foreign key constraint`, `is still referenced` | `23001`, `violates RESTRICT setting of foreign key constraint`, `is referenced` |
| `NO ACTION` | `23503` | `23503` |

The [release notes for version 18](https://www.postgresql.org/docs/18/release-18.html) list it among the incompatibilities: `RESTRICT` violations are now reported "with SQLSTATE 23001 (restrict_violation) instead of 23503". An application that turns 23503 into "this customer still has invoices" now sends a `RESTRICT` refusal down its generic error path, a 500 where there used to be a friendly message. Rails is one example: its PostgreSQL adapter maps 23503 to `ActiveRecord::InvalidForeignKey` and has no mapping for 23001, so a `rescue ActiveRecord::InvalidForeignKey` around a delete no longer catches a `RESTRICT` key on PostgreSQL 18. The message text changed too, so a check that matches "still referenced" breaks the same way. Before upgrading, search for 23503, `foreign_key_violation` and your framework's foreign key exception, and add 23001 next to them.

## What do MySQL, SQL Server and SQLite do with RESTRICT and NO ACTION?

MySQL and MariaDB treat them as the same action, SQL Server has no `RESTRICT`, and SQLite keeps PostgreSQL's distinction.

| | `RESTRICT` | `NO ACTION` | `SET DEFAULT` |
|---|---|---|---|
| PostgreSQL 18.3 | 23001, never deferred | 23503, can be deferred | Resets the column |
| MySQL 8.4.11 (InnoDB) | 1451 | 1451, checked immediately | Kept in the schema, refused with 1451 |
| MariaDB 11.4.13 (InnoDB) | 1451 | 1451 | Stored as `RESTRICT` |
| SQL Server 2022 | `Incorrect syntax near the keyword 'RESTRICT'` | Error 547 | Resets the column |
| SQLite 3.54 | Refused, never deferred | Refused, can be deferred | Resets the column |

MySQL's `SET DEFAULT` is the trap in that table. The MySQL 8.4 manual says InnoDB rejects table definitions containing `ON DELETE SET DEFAULT`, but 8.4.11 did not: `CREATE TABLE` accepted the clause, `SHOW CREATE TABLE` printed it back, and `information_schema.REFERENTIAL_CONSTRAINTS` listed `DELETE_RULE` as `SET DEFAULT`. Then deleting the customer failed with error 1451, and the invoice still pointed at it. Everything that reads the schema says the column resets; nothing ever resets it. MariaDB at least tells the truth after the fact: it stored the same clause as `RESTRICT`. SQL Server did what the clause says and set the invoice's `customer_id` to its default, 0.

## Should you use RESTRICT or NO ACTION?

Use either to stop a parent from being deleted while children exist; pick `NO ACTION` when jobs need to repair the children inside a statement or a deferred transaction, and `RESTRICT` when nothing should ever get that chance.

| You want | Use |
|---|---|
| A parent that cannot be deleted while children exist | Either; `RESTRICT` states the intent, `NO ACTION` is the default |
| Jobs that delete a parent and repoint its children in one transaction | `NO ACTION` on a `DEFERRABLE` key |
| A refusal even inside a statement or transaction that would fix the children | `RESTRICT` |
| The same schema on SQL Server | `NO ACTION`: `RESTRICT` is a syntax error there |
| Children reset to a default on MySQL or MariaDB | Not available: `SET DEFAULT` behaves as `RESTRICT`; use `SET NULL` or application code |

A key on invoices that should never lose its customer reads best as `RESTRICT`, which is what [ON DELETE SET NULL vs CASCADE](https://schemity.com/blog/postgres-on-delete-set-null-vs-cascade/) recommends for records people rely on. The cost on PostgreSQL 18 is one more error code to catch, not a reason to choose differently.

## How does Schemity keep these actions honest across databases?

Schemity offers each database only the actions it carries out, and says what it wrote instead when a design moves between them.

Schemity is database design software that reads your live database, shows the impact of every schema change before it runs, and keeps the diagram as a file in Git. Its [relationship](https://schemity.com/doc/relationships/) dialog lists the actions per database: `RESTRICT` is not offered on SQL Server, and `SET DEFAULT` is not offered on MySQL or MariaDB. A MySQL key imported from SQL or read from the database with `SET DEFAULT` keeps it, and [schema lint](https://schemity.com/doc/schema-lint/) (a Schemity Pro feature) reports it as failing at runtime: "MySQL accepts SET DEFAULT and then refuses the change in customers, as RESTRICT does", naming error 1451. When a design is converted to another database (also Pro), a `RESTRICT` key converted to SQL Server becomes `NO ACTION` with a note, and a `SET DEFAULT` key converted to MySQL or MariaDB becomes `RESTRICT` with a note saying why. That is how you see the impact of every change in a key's action before it reaches a database that would quietly do something else.

![Schemity's lint drawer on a MySQL diagram of customers and invoices: one finding that fails at runtime on invoices.customer_id, key invoices_customer_fk, MySQL accepts SET DEFAULT and then refuses the change in customers, as RESTRICT does, with the detail that InnoDB never writes the default, so a delete in customers with invoices pointing at it fails with error 1451](https://schemity.com/images/blog/on-delete-restrict-vs-no-action/lint-mysql-set-default.webp)

Writing this post found three wording problems, fixed for the next release: on a MariaDB diagram the lint finding still said "MySQL"; the conversion note claimed InnoDB rejects `SET DEFAULT`, when MySQL accepts the clause and only refuses the delete; and a conversion to MariaDB did not say that MariaDB stores the clause as `RESTRICT`.

## Related reading

- [SQL Server multiple cascade paths, error 1785](https://schemity.com/blog/sql-server-multiple-cascade-paths-error-1785/) shows when SQL Server forces a key back to `NO ACTION`.
- [Should you use foreign key constraints](https://schemity.com/blog/should-you-use-foreign-key-constraints/) asks the question before any of these.

## Frequently asked questions

### `ON DELETE RESTRICT` vs `NO ACTION`: what is the difference?

Both refuse a delete that would leave child rows pointing at nothing. `NO ACTION`, the default, checks at the end of the statement, or at commit when the key is deferred, so the same statement or transaction can put a parent back or repoint its children before the check runs. `RESTRICT` checks immediately and cannot be deferred. On PostgreSQL 18 and later they also raise different error codes: 23001 for `RESTRICT`, 23503 for `NO ACTION`.

### Why did my foreign key error handler stop working on PostgreSQL 18?

PostgreSQL 18 reports a violated `RESTRICT` action as SQLSTATE 23001, `restrict_violation`, instead of 23503, `foreign_key_violation`, and the release notes list it among the incompatibilities of version 18. A handler that recognises a referenced parent by 23503 now misses deletes refused by `RESTRICT` keys. Rails' PostgreSQL adapter, for one, maps only 23503 to `ActiveRecord::InvalidForeignKey`. Check for 23001 as well as 23503.

### Does MySQL support `ON DELETE SET DEFAULT`?

It accepts the clause and does not carry it out. MySQL 8.4 keeps `ON DELETE SET DEFAULT` in `SHOW CREATE TABLE` and `information_schema`, then refuses the parent delete with error 1451, exactly as `RESTRICT` would. MariaDB 11.4 stores the same clause as `RESTRICT`. SQL Server 2022 and PostgreSQL do carry it out.
