All rules

Schema

postgresql/require-fk-include-columns

Require every foreign-key constraint to include a configured set of columns.

  • Type problem
  • Recommended off
  • Fixable no

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

Incorrect
CREATE TABLE orders (
  tenant_id bigint NOT NULL,
  id        bigserial PRIMARY KEY,
  user_id   bigint REFERENCES users (id)
);
Incorrect
ALTER TABLE orders
  ADD CONSTRAINT orders_user_fk
  FOREIGN KEY (user_id) REFERENCES users (id);

Correct

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)
);
Correct
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

columns string[]
Required. Column names that every foreign-key constraint must include in its referencing column list.
excludeTablePattern string (regex)
Skip the check for any table whose name matches this pattern. Useful for audit / log tables that intentionally fan in across tenants.
excludeReferencedTablePattern string (regex)
Skip the check for any FK whose referenced (`REFERENCES`) table matches this pattern. Typically used to exclude the tenant table itself and global lookup tables that are shared across tenants.
Try this rule

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.

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

No issues found.

2 rules enabled.

Rule under test require-fk-include-columns — 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