Skip to content

MG02 — ADD CONSTRAINT (FK/CHECK) without NOT VALID validates under a lock

Group: Migration safety · Auto-fixable: yes

Adding a FOREIGN KEY or CHECK constraint scans and validates every existing row while holding a lock. Add it NOT VALID first, then VALIDATE CONSTRAINT in a separate statement, which takes a weaker lock.

bad: ALTER TABLE t ADD CONSTRAINT c CHECK (a > 0);
good: ALTER TABLE t ADD CONSTRAINT c CHECK (a > 0) NOT VALID;

Disable this rule in banshee.toml:

[lint]
exclude = ["MG02"]

Or silence one line with -- noqa: MG02. See Linting.