fix(postgres): populate fields, value and reltype in ForeignKeyConstraintError#18129
fix(postgres): populate fields, value and reltype in ForeignKeyConstraintError#18129topmonroe9 wants to merge 1 commit intosequelize:mainfrom
Conversation
…raintError Closes sequelize#7826 The PostgreSQL dialect was hardcoding `fields: null` in ForeignKeyConstraintError even though the error detail string contains the constraint fields and values. This made it impossible for users to build meaningful error messages from FK violations. Parse `err.detail` (e.g. `Key (user_id)=(42) is not present in table "users"`) to extract field names and values, and determine the relationship type from the error message.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe PR enhances PostgreSQL foreign key constraint error handling by extracting detailed metadata—including specific field names and relationship type (parent/child)—from database errors and including them in the ForeignKeyConstraintError, replacing previously null values. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 OpenGrep (1.16.1)packages/core/test/integration/error.test.ts┌──────────────┐ �[32m✔�[39m �[1mOpengrep OSS�[0m �[1m Loading rules from local config...�[0m packages/postgres/src/query.js┌──────────────┐ �[32m✔�[39m �[1mOpengrep OSS�[0m �[1m Loading rules from local config...�[0m Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Looks like the db2 failure is in |
Summary
Closes #7826
The PostgreSQL dialect was hardcoding
fields: nullwhen constructingForeignKeyConstraintError, even thougherr.detailcontains the offending field names and values. This made it impossible for developers to build user-friendly error messages from FK constraint violations — a long-standing complaint on the issue.Before:
After:
Changes
packages/postgres/src/query.js— parseerr.detailto extract FK field names and values via theKey (field)=(value)pattern already used for unique constraint errors (23505). Determinereltypefrom the error message (insert or update→ child,update or delete→ parent).packages/core/test/integration/error.test.ts— update postgres assertions to expect populatedfieldsandreltypeTest plan
Summary by CodeRabbit