Skip to content

[Backend] loan_disputes foreign key references loan_events(loan_id), which has no total UNIQUE constraint #1191

Description

@grantfox-oss

Telegram (ask questions / claim the issue here first): https://t.me/+DOylgFv1jyJlNzM0

Why this matters

migrations/1784000000014_add-loan-disputes.js:9 declares loan_id INTEGER NOT NULL REFERENCES loan_events(loan_id). loan_events.loan_id (migrations/1771691269866_loan-events-schema.js:16) is nullable with only a plain index; the only unique constraints touching loan_id are composite (loan_id,event_type,ledger) or PARTIAL (WHERE event_type=LoanApproved), none of which a FK can target. Postgres rejects this with there is no unique constraint matching given keys for referenced table loan_events. The model is also wrong: loan_events is an append-only event log with many rows per loan_id.

Acceptance criteria

  • Reference a table/column with a real unique key (e.g. a loans summary table keyed by loan_id) or drop the DB-level FK and validate loan_id in application code
  • Confirm the disputes migration applies cleanly after the loan_events->contract_events rename
  • Add an integration test that inserts a dispute against the real schema

Files to touch

  • backend/migrations/1784000000014_add-loan-disputes.js
  • backend/migrations/1771691269866_loan-events-schema.js

Out of scope

  • Dispute resolution business logic

Metadata

Metadata

Labels

Stellar WaveIssues in the Stellar wave programbackendIssues related to backend developmentbugSomething isn't workingcriticalHigh-impact / hard issuedatabaseDatabase related issues

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions