Skip to content

Fix ERD delete crash on a stale foreign-key column (#8318)#10042

Merged
asheshv merged 2 commits into
pgadmin-org:masterfrom
dpage:fix-8318-erd-delete-crash
Jun 12, 2026
Merged

Fix ERD delete crash on a stale foreign-key column (#8318)#10042
asheshv merged 2 commits into
pgadmin-org:masterfrom
dpage:fix-8318-erd-delete-crash

Conversation

@dpage

@dpage dpage commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #8318.

Deleting a table or relationship link in the ERD tool could fail with i.default.find(...) is undefined (a removeOneToManyLink stack trace), blocking the delete.

Root cause: removeOneToManyLink (ERDCore.js:529) did _.find(tableNode.getColumns(), (col)=>col.name==theFk.local_column).attnum. If a column was renamed, the FK's stored local_column no longer matches any current column, so _.find returns undefined and .attnum throws — and the link removal code below it never runs.

Fix: use optional chaining (?.attnum). A stale FK then simply doesn't match the link's attnum, and the link is removed as expected.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue in the ERD tool where deleting a table or relationship link would fail when a foreign key referenced a renamed column.
  • Documentation

    • Updated release notes documenting the ERD tool fix for handling renamed column references.

@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 92d7ca99-6abd-4ea8-abc0-7fc3cbd41a91

📥 Commits

Reviewing files that changed from the base of the PR and between ee6e8dc and 5d4b12b.

📒 Files selected for processing (2)
  • docs/en_US/release_notes_9_16.rst
  • web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js

Walkthrough

This PR fixes issue #8318 by adding null-safe optional chaining to the ERD tool's removeOneToManyLink function when looking up column attributes, preventing crashes during link deletion after column renames. The fix is documented in the v9.16 release notes.

Changes

ERD Tool Bug Fix and Release Documentation

Layer / File(s) Summary
Null-safe column lookup in ERD link removal
web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js
removeOneToManyLink now uses optional chaining (?.attnum) when computing the attribute number for a foreign key's local column, preventing crashes when the column lookup returns undefined instead of a column object.
Release notes documentation for ERD bug fix
docs/en_US/release_notes_9_16.rst
pgAdmin 4 v9.16 release notes updated to document issue #8318, describing the fix for ERD deletion errors when a foreign key references a renamed column.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

  • pgadmin-org/pgadmin4#9581: Refactors ERD foreign-key/one-to-many link creation by adjusting how attnum/local FK column references are resolved, directly related to this PR's null-safe column lookup fix.

Suggested reviewers

  • asheshv
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main change: fixing a crash in the ERD tool when deleting items that have stale foreign-key column references, with direct reference to the issue being fixed.
Linked Issues check ✅ Passed The PR successfully addresses issue #8318 by implementing optional chaining (?.attnum) in removeOneToManyLink to handle stale foreign-key columns, preventing crashes during table/link deletion as required.
Out of Scope Changes check ✅ Passed All changes are within scope: ERDCore.js fix targets the crash bug, release notes document the fix, and toctree entry properly references the version notes—no extraneous modifications found.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js (1)

379-379: 💤 Low value

Consider applying optional chaining to other column lookups.

While the fix on line 529 correctly addresses the reported deletion crash, there are several other locations in this file where _.find(...).attnum or _.find(...).name patterns are used without optional chaining. These could potentially crash in similar scenarios:

  • Line 379: _.find(tableData.columns, ...).name
  • Lines 412-413: _.find(...).attnum in addLink
  • Lines 475-476: _.find(...).name in addOneToManyLink
  • Line 506: _.find(...).name
  • Lines 712-715, 718-720: _.find(...).attnum in addLinksBetweenNodes

Consider adding optional chaining (?.) to these lookups as well, with appropriate fallback handling when columns are not found (e.g., early return, validation error, or skipping the operation).

Also applies to: 412-413, 475-476, 506-506, 712-720

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js` at line 379, Several
lookups like fkColumn.referenced = _.find(tableData.columns,
(colm)=>colm.attnum==attnum).name and other similar patterns in addLink,
addOneToManyLink, and addLinksBetweenNodes can throw when _.find returns
undefined; update each of these to use optional chaining (e.g., _.find(...)?
.name or ? .attnum) and add a safe fallback/validation: if the find returns
undefined then either return early, log/throw a clear validation error, or skip
the operation so the code won’t crash; specifically change the
fkColumn.referenced assignment and all _.find(...).attnum/.name usages inside
addLink, addOneToManyLink, addLinksBetweenNodes to use ?., check the result, and
handle the missing-column case consistently.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js`:
- Line 379: Several lookups like fkColumn.referenced = _.find(tableData.columns,
(colm)=>colm.attnum==attnum).name and other similar patterns in addLink,
addOneToManyLink, and addLinksBetweenNodes can throw when _.find returns
undefined; update each of these to use optional chaining (e.g., _.find(...)?
.name or ? .attnum) and add a safe fallback/validation: if the find returns
undefined then either return early, log/throw a clear validation error, or skip
the operation so the code won’t crash; specifically change the
fkColumn.referenced assignment and all _.find(...).attnum/.name usages inside
addLink, addOneToManyLink, addLinksBetweenNodes to use ?., check the result, and
handle the missing-column case consistently.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 668d2817-8250-49d8-ae30-c3d06fda8f01

📥 Commits

Reviewing files that changed from the base of the PR and between 937d34e and ee6e8dc.

📒 Files selected for processing (3)
  • docs/en_US/release_notes.rst
  • docs/en_US/release_notes_9_16.rst
  • web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js

@dpage dpage force-pushed the fix-8318-erd-delete-crash branch 2 times, most recently from 0842cfe to 16cd6de Compare June 9, 2026 11:36
removeOneToManyLink looked up a column by the FK's stored local_column
name and read .attnum unconditionally. After a column rename the stored
name no longer matches, so _.find returned undefined and .attnum threw,
blocking deletion of the table/link. Use optional chaining so a stale FK
simply doesn't match the link being removed and deletion proceeds.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@dpage dpage force-pushed the fix-8318-erd-delete-crash branch from 16cd6de to 7c8a44a Compare June 9, 2026 11:37
@asheshv asheshv requested a review from Copilot June 10, 2026 14:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an ERD tool crash that occurred when deleting a table or relationship link after a foreign-key column rename left stale local_column metadata, and documents the fix in the v9.16 release notes.

Changes:

  • Prevent removeOneToManyLink from throwing when the FK’s local_column no longer matches any current column (optional chaining on the _.find(...).attnum access).
  • Add a v9.16 release note entry referencing Issue #8318.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js Avoids a delete-time crash when FK column metadata is stale by safely handling missing column lookups.
docs/en_US/release_notes_9_16.rst Adds release note entry for the ERD delete crash fix (Issue #8318).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread web/pgadmin/tools/erd/static/js/erd_tool/ERDCore.js

@asheshv asheshv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crash fix is correct — happy to approve.

Worth filing a follow-up though: the underlying cause is syncFkRefNames only syncing the referenced side, not local_column when a local column is renamed, so the stale row continues to silently disappear from the model on delete rather than be matched and removed properly. There's also a sibling unguarded .name deref at the same function (line 379) that has the same shape.

A regression test exercising the stale-column delete path on ERDCore.removeOneToManyLink would help — there's no coverage for it today.

@asheshv asheshv merged commit 2a34eda into pgadmin-org:master Jun 12, 2026
32 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

i.default.find(...) is undefined when trying to delete table or link in ERD

3 participants