ReviewOS

stacks/bun-query-builder

Migration diff ignores foreign-key (references) changes — columnsAreDifferent omits references

#1037
Closed glennmichael123 opened this 2 months ago ·
2 months ago

Summary

columnsAreDifferent compares type/nullable/default/unique/enum but never col.references. Adding, removing, or changing a FK target or onDelete/onUpdate on an existing column produces no diff, so generateDiffSql emits nothing.

Evidence (repro)

belongsTo: ['User'] -> belongsTo: [{ model:'User', onDelete:'cascade' }]  =>  "-- No changes detected"

Location

src/migrations.ts:~895-917.

Suggested fix

Compare references (table/column/onDelete/onUpdate) in columnsAreDifferent and emit addForeignKey/drop-constraint statements on diff. (SQLite can't ALTER ... ADD CONSTRAINT — needs the table-rebuild path or a documented limitation.)

Severity: medium/high. Found via internal code audit.

8 days ago

Fixed in 04e2629. Added referencesAreDifferent() and, in the migration diff's modify-column loop, emit addForeignKey when a column's FK reference changes (target/onDelete/onUpdate added or modified) — independent of column attributes, since an FK-only change leaves type/nullable identical. So belongsTo: ['User'] → [{ model:'User', onDelete:'cascade' }] now produces an FK statement instead of "No changes detected". Regression test added; full suite green. (Dropping the old constraint + SQLite's ALTER-ADD-CONSTRAINT limitation noted as follow-ups.)

Sign in to comment on this issue.