Skip to content

require-fk-index

Cascading or set-value FOREIGN KEY action lacks a child-table index.

Why

PostgreSQL does not automatically index referencing columns, so cascading deletes/updates and SET NULL/DEFAULT actions may scan and lock the child table.

Fix

Create an index whose leading columns cover the foreign-key columns on the child table.

Examples

Before — flagged Foreign key without a child-table index
migrations/001_orders.sql
CREATE TABLE orders (
customer_id BIGINT REFERENCES customer (id) ON DELETE CASCADE
);
After — preferred Foreign key covered by an index
migrations/001_orders.sql
CREATE TABLE orders (
customer_id BIGINT REFERENCES customer (id) ON DELETE CASCADE
);
CREATE INDEX orders_customer_id_idx ON orders (customer_id);