Why this matters
Prevents orphaned rows and improves join performance.
Enforce referential integrity with add_foreign_key and matching indexes.
Prevents orphaned rows and improves join performance.
Side-by-side examples engineers can pattern-match during review.
add_column :orders, :user_id, :bigintadd_reference :orders, :user, foreign_key: true, index: trueuser_id without FKadd_foreign_key :orders, :usersFrom the same buckets as this rule.
Review SQL/database migrations for operations that can lock large tables or cause downtime. Examples: creating indexes without CONCURRENTLY (Postgres), ALTER COLUMN TYPE on big tables, adding NOT NULL without backfill, long-running updates without batching. Require an online migration strategy (CONCURRENTLY, backfill in batches, dual-write/expand-contract) and a rollback plan.