← all rules

forbid-nullable-fk

warning

Nullable foreign key

Why it's unsafe

A foreign-key column is nullable; a nullable foreign key allows orphan rows and unexpected join results.

Safe rewrite

Add NOT NULL to the foreign-key column (inline or a later SET NOT NULL), or suppress if a nullable foreign key is intended.

Example

Try it in the playground →

Unsafe

CREATE TABLE orders (id bigint PRIMARY KEY, customer_id bigint REFERENCES customers (id));

Safe

CREATE TABLE orders (id bigint PRIMARY KEY, customer_id bigint NOT NULL REFERENCES customers (id));

Related