All rules
Safety
postgresql/
Enforce a consistent stance on `NOT VALID` for `ALTER TABLE ... ADD FOREIGN KEY`.
Why this matters
Adding a foreign key normally validates every existing row under an `ACCESS EXCLUSIVE` lock that blocks writers. The safe pattern is to `ADD ... NOT VALID` (metadata-only) and then `VALIDATE CONSTRAINT` in a separate migration; `VALIDATE` only takes a `SHARE UPDATE EXCLUSIVE` lock. Some projects prefer the inverse — fail loudly at constraint-add time — and can flip this rule with the `style` option.
Examples
Incorrect
ALTER TABLE orders
ADD CONSTRAINT orders_customer_fk FOREIGN KEY (customer_id) REFERENCES customers (id);Correct
ALTER TABLE orders
ADD CONSTRAINT orders_customer_fk FOREIGN KEY (customer_id) REFERENCES customers (id) NOT VALID;ALTER TABLE orders VALIDATE CONSTRAINT orders_customer_fk;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/consistent-fk-not-valid": [
"warn",
{
style: "always",
},
],
},
},
]; Options
Edit the SQL — only consistent-fk-not-valid is enabled.
Pre-filled with the first incorrect example. Toggle off in the rule shelf to see how the diagnostic disappears.
Diagnostics
No issues found.
2 rules enabled.
Rule under test
consistent-fk-not-valid — plus no-syntax-error as a safety net.