All rules

Safety

postgresql/require-on-delete-action

Require an explicit `ON DELETE` clause on every foreign-key constraint.

  • Type suggestion
  • Recommended warn
  • Fixable no

Why this matters

Without `ON DELETE`, the default is `NO ACTION` — but it is rarely the deliberate choice. Forcing the author to write the action makes intent visible at the call site. Covers both inline `REFERENCES` and `ALTER TABLE ADD CONSTRAINT FOREIGN KEY`. With the `allowed` option, the rule additionally fails when the action is not in an opt-in list.

Examples

Incorrect

Incorrect
CREATE TABLE t (
  fid integer REFERENCES other(id)
);
Incorrect
ALTER TABLE t ADD CONSTRAINT fk FOREIGN KEY (fid) REFERENCES other(id);

Correct

Correct
CREATE TABLE t (
  fid integer REFERENCES other(id) ON DELETE RESTRICT
);
Correct
ALTER TABLE t ADD CONSTRAINT fk FOREIGN KEY (fid) REFERENCES other(id) ON DELETE SET NULL;

Configure it

// eslint.config.js
import postgresql from "eslint-plugin-postgresql";

export default [
  {
    files: ["**/*.sql"],
    languageOptions: {
      parser: postgresql.configs.recommended.languageOptions.parser,
    },
    plugins: { postgresql },
    rules: {
      "postgresql/require-on-delete-action": [
        "warn",
        {
          allowed: [],
        },
      ],
    },
  },
];

Options

allowed ("CASCADE" | "RESTRICT" | "NO ACTION" | "SET NULL" | "SET DEFAULT")[]
When set, the rule additionally flags any `ON DELETE` whose action is not in this list. Use it to bake project-wide policy into the rule (e.g. `["RESTRICT", "NO ACTION"]` to forbid cascading deletes).
Try this rule

Edit the SQL — only require-on-delete-action is enabled.

Pre-filled with the first incorrect example. Toggle off in the rule shelf to see how the diagnostic disappears.

0 errors 0 warnings parse 0ms · rules 0ms
Diagnostics

No issues found.

2 rules enabled.

Rule under test require-on-delete-action — plus no-syntax-error as a safety net.
eslint-plugin-postgresql

An ESLint plugin that lints .sql files with real PostgreSQL grammar and a curated set of best-practice rules.

© 2026 eslint-plugin-postgresql contributors Built on libpg-query · PostgreSQL 17