Schema
postgresql/
Require every foreign-key constraint to include a configured set of columns.
Why this matters
Useful in multi-tenant schemas where every FK must carry the tenant key (e.g. `tenant_id`) so a child row cannot point at a parent row in a different tenant. The rule covers inline column-level FKs (`col integer REFERENCES other(id)`), table-level FKs inside `CREATE TABLE`, and `ALTER TABLE ADD CONSTRAINT ... FOREIGN KEY`. The `columns` option is required and lists the column names that every FK must include. `excludeTablePattern` and `excludeReferencedTablePattern` are regex strings for opting tables out — typically the tenant table itself (`tenants`) and global lookup tables.
Examples
Incorrect
CREATE TABLE orders (
tenant_id bigint NOT NULL,
id bigserial PRIMARY KEY,
user_id bigint REFERENCES users (id)
);ALTER TABLE orders
ADD CONSTRAINT orders_user_fk
FOREIGN KEY (user_id) REFERENCES users (id);Correct
CREATE TABLE orders (
tenant_id bigint NOT NULL,
id bigint NOT NULL,
user_id bigint NOT NULL,
PRIMARY KEY (tenant_id, id),
FOREIGN KEY (tenant_id, user_id) REFERENCES users (tenant_id, id)
);ALTER TABLE orders
ADD CONSTRAINT orders_user_fk
FOREIGN KEY (tenant_id, user_id) REFERENCES users (tenant_id, id);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-fk-include-columns": [
"warn",
{
columns: [],
excludeTablePattern: "",
excludeReferencedTablePattern: "",
},
],
},
},
]; Options
Edit the SQL — only require-fk-include-columns is enabled.
Pre-filled with the first incorrect example. Toggle off in the rule shelf to see how the diagnostic disappears.
No issues found.
2 rules enabled.
require-fk-include-columns — plus no-syntax-error as a safety net.