Skip to content

Database: the Changes scope keeps what a changed row points at - #127

Merged
agorischek merged 2 commits into
mainfrom
agorischek/db-changes-context
Nov 21, 2026
Merged

agorischek merged 2 commits into
mainfrom
agorischek/db-changes-context

Conversation

@agorischek

@agorischek agorischek commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Found by the gallery session. When only a foreign key's properties change (for example its onDelete), the Changes scope hides the referenced table, so the modified key has nothing to link to. This is the same class of bug as #83: Changes must keep what a changed row needs to be read, and a foreign key needs both ends.

Cause. In Prisma and Django extractions a foreign key is a constraint entity:

  • it references its target table;
  • it constrains its own columns;
  • the link itself is drawn from a column-to-column foreign-key relationship.

The shell's Changes context keeps a changed entity's containers (the key's own table) and a changed relationship's endpoints. A key whose only change is a property has no changed relationship, so the scope never reached the referenced table.

Fix. The database renderer gets a scope context, databaseContext in src/scope.ts. From each changed row it follows references, constrains, foreign-key, relation and enum-type outward. So a changed key keeps:

  • its referenced table;
  • its own columns;
  • the columns they reference;
  • the link between them.

A changed column likewise keeps the column or enum it refers to. Every other column still folds.

Changes scope, prisma-shop with only Order.customerId's onDelete changed:

Light: before, Order alone with the key pointing nowhere; after, Customer kept with the link
Dark

Test. A unit test in database/test/diff.test.mjs narrows the model exactly as the shell does (changesScope, then the context, then scopedModel with folded parts) and lays out the diff. It checks that Customer is kept and that the key's link runs Order → Customer. Without the fix it fails, keeping only Order.

Note, not changed here. A single-column key is drawn as its column's FK marker. In that form its change shows only as the amber marker and status icon, with no "onDelete changed" line, and the All scope shows it the same way. Before this fix, Changes showed the key as a separate row with that line only because its column was hidden.

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.

🟡 Changes recommended

For property-only changes to Order.customerId, the renderer-context traversal omits the owning Customer table, causing the Order → Customer link to disappear from the narrowed model.

1 open issue
What changed in this PR

This PR extends the database renderer’s Changes scope to retain the context needed to read changed rows, addressing missing foreign-key links when only constraint properties change.

Changes:

  • Adds databaseContext to follow key, column, relation, and enum references.
  • Connects this context to the renderer’s folded Changes scope.
  • Adds a regression test for an onDelete-only change, checking that the referenced table and link remain visible.
File Description
packages/​renderers/​database/​test/​diff.test.mjs Tests referenced-table and link retention after a foreign-key property change.
packages/​renderers/​database/​src/​scope.ts Defines database-specific relationship traversal for scope context.
packages/​renderers/​database/​src/​main.tsx Registers databaseContext with the renderer shell.

Review effort: Balanced

agorischek and others added 2 commits November 20, 2026 19:21
When only a foreign key's properties changed (its onDelete, say), the Changes scope kept the key's own table but not
the table it references, so the key had no link. The renderer's scope context now follows a changed row's
references, constrains, foreign-key, relation and enum-type relationships: a changed key keeps its referenced table,
its columns and the columns they reference, with the link between them, and a changed column the column or enum it
refers to.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
A column-only change reached the referenced column through its foreign-key link but not that column's table: the
shell places only its own context in containers. databaseContext now adds the containers of what it reaches, in either
revision. A column-only regression joins the key-only one.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@agorischek
agorischek force-pushed the agorischek/db-changes-context branch from b0b24a9 to b7d4b43 Compare November 21, 2026 03:22
@agorischek
agorischek merged commit f40836c into main Nov 21, 2026
37 checks passed
@agorischek
agorischek deleted the agorischek/db-changes-context branch November 21, 2026 04:10
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.

2 participants