All rules

Schema

postgresql/no-composite-primary-key

Disallow composite (multi-column) PRIMARY KEY constraints.

  • Type problem
  • Recommended off
  • Fixable no

Why this matters

A composite PRIMARY KEY couples a row's identity to multiple business columns. Every foreign-key reference must repeat that column set, ORMs frequently misbehave on multi-column keys, and changing the natural key later requires rewriting both the table and every referrer. Use a single surrogate key (e.g. `id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY` or a UUID) and enforce the natural uniqueness with a `UNIQUE` constraint on the same columns.

Examples

Incorrect

Incorrect
CREATE TABLE memberships (user_id bigint, group_id bigint, PRIMARY KEY (user_id, group_id));
Incorrect
ALTER TABLE memberships ADD CONSTRAINT memberships_pkey PRIMARY KEY (user_id, group_id);

Correct

Correct
CREATE TABLE memberships (id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, user_id bigint NOT NULL, group_id bigint NOT NULL, UNIQUE (user_id, group_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/no-composite-primary-key": "warn",
    },
  },
];

Options

This rule has no options.

Try this rule

Edit the SQL — only no-composite-primary-key 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 no-composite-primary-key — 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