Skip to content

fix(designer): show the foreign keys a table already has - #551

Merged
EVWorth merged 1 commit into
mainfrom
fix/designer-foreign-keys
Sep 7, 2026
Merged

EVWorth merged 1 commit into
mainfrom
fix/designer-foreign-keys

Conversation

@EVWorth

@EVWorth EVWorth commented Sep 5, 2026

Copy link
Copy Markdown
Owner

Closes #385, #386.

The tab was empty for every table — #386

ForeignKeyInfo existed in the inspector with nothing producing it, no command exporting it and no binding for it. So an existing constraint couldn't be seen, edited or removed, and the diff baseline was a hardcoded empty list.

get_foreign_keys joins KEY_COLUMN_USAGE (one row per column) to REFERENTIAL_CONSTRAINTS (one row per constraint, carrying the ON UPDATE / ON DELETE rules). Rows are grouped by constraint in ORDINAL_POSITION order, so a composite key keeps its column order — the pairing with the referenced columns is positional, and the wrong order builds a different constraint.

ForeignKeyInfo took a single column and referenced_column, which can't describe a composite key. It carries both as lists now; nothing consumed the old shape, so nothing had to be migrated.

The designer loads them into the form and the baseline, so an untouched key is no change, and editing ON DELETE emits one DROP plus one ADD rather than an unexplained ADD.

Correction to the issue: its repro ends "the original RESTRICT FK is dropped (lost)". It isn't — with an empty baseline the diff emits only an ADD, and nothing drops the original. The real harm is that the constraint is invisible and unmanageable, which is enough on its own.

Mismatched types reached the server — #385

MySQL answers INT REFERENCES VARCHAR(50) with ERROR 1215, naming neither the column nor the reason. The editor let any local column point at any referenced column, so that error was the first sign.

foreignKeyTypeProblem compares type families — integer, decimal, text, binary, temporal — and says which pair can't work.

Deliberately a warning, not a gate. The real rules take in storage engine, character set, index coverage and signedness; a check that refuses more than the server would is a worse failure than one that occasionally lets something through. An unrecognised type on either side produces no claim rather than a guess.

Referenced columns now carry their type. A key loaded from the server names a table nobody picked, so the tab requests that table's columns itself — once per table, since the loader swallows failures and keying off "not loaded yet" alone would retry on every render.

Tests

Four integration tests, two of them run against both servers: the actions come back right, a composite key keeps its column order, and a table with no keys reports none. Six unit tests on the compatibility check, four on the component — the three asserting new behaviour fail against the previous version.

54 integration tests against live servers, 1776 unit + 298 browser, clippy/tsc/dprint/eslint clean.

🤖 Generated with Claude Code

Closes #385, #386.

## The tab was empty for every table (#386)

ForeignKeyInfo existed in the inspector with nothing producing it, no
command exporting it and no binding for it — so an existing constraint could
not be seen, edited or removed, and the diff baseline was a hardcoded empty
list.

get_foreign_keys joins KEY_COLUMN_USAGE, which has one row per column, to
REFERENTIAL_CONSTRAINTS, which has one row per constraint and carries the ON
UPDATE and ON DELETE rules. Rows are grouped by constraint in ORDINAL_
POSITION order, so a composite key keeps its column order — the pairing with
the referenced columns is positional, and the wrong order would build a
different constraint.

ForeignKeyInfo took a single column and a single referenced column, which
cannot describe a composite key. It carries both as lists now. Nothing
consumed the old shape, so nothing had to be migrated.

The designer loads them into the form and into the baseline, so an untouched
key is no change and editing ON DELETE emits one DROP and one ADD rather
than an unexplained ADD.

The issue's repro ends "the original RESTRICT FK is dropped (lost)". It is
not: with an empty baseline the diff emits only an ADD, and nothing drops
the original. The harm is that the constraint is invisible and unmanageable,
which is enough.

## Mismatched types reached the server (#385)

MySQL answers `INT REFERENCES VARCHAR(50)` with ERROR 1215, which names
neither the column nor the reason. The editor let any local column point at
any referenced column, so that error was the first sign.

foreignKeyTypeProblem compares type families — integer, decimal, text,
binary, temporal — and says which pair cannot work. Deliberately a warning
and not a gate: the real rules take in storage engine, character set, index
coverage and signedness, and refusing more than the server would is the
worse failure. An unrecognised type on either side produces no claim rather
than a guess.

Referenced columns now carry their type, and a key loaded from the server
names a table nobody picked, so the tab asks for that table's columns
itself — once per table, since the loader swallows failures and keying off
"not loaded yet" would retry every render.

## Tests

Four integration tests, two of them per server, covering the actions, a
composite key's column order, and a table with no keys at all. Six unit
tests on the compatibility check and four on the component; the three
component tests asserting new behaviour fail against the previous version.

Co-Authored-By: Claude Opus 5 <[email protected]>
@EVWorth
EVWorth merged commit 72f3e55 into main Sep 7, 2026
10 checks passed
@EVWorth
EVWorth deleted the fix/designer-foreign-keys branch September 7, 2026 15:54
EVWorth added a commit that referenced this pull request Sep 9, 2026
Closes #369, #370, #371, #372, #373, #374.

Schema compare could not back the claim it made, and a compare tool that is
quietly wrong is worse than none, because people act on its answer.

  - SchemaSnapshot had no foreignKeys field, so two schemas differing only in
    their foreign keys compared as identical (#370)
  - DDL was normalised by lowercasing and collapsing whitespace before being
    compared, which mangles string literals and identifiers — reporting
    differences that are not there and missing ones that are (#371)
  - column order was ignored, so a reorder read as no change (#374)
  - the sync half ran each statement independently, leaving a target
    half-migrated when one failed (#369), and built `USE ${database}` by raw
    string concatenation (#372)

Fixing all of that is a real piece of work — a proper DDL comparison rather
than string normalisation is most of it — and the feature is not part of how
this project is used. Removing it now is cheaper than carrying it, and the
history has it if someone wants it back.

Gone: SchemaCompare, SyncPreview, schema-diff, sync-sql-generator, their
tests, the "compare" editor tab type, and the entry points in the menu bar,
title bar and toolbar. 840 lines of feature, about 1,100 with tests.

Two things kept deliberately. The test asserting that right-clicking a
toolbar button does not open the system menu now right-clicks Admin — it used
Compare only because it was there, and the behaviour still matters. And
get_foreign_keys stays: the designer uses it, and it was written for the
designer in #551.

Co-authored-by: Claude Opus 5 <[email protected]>
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.

[P1][bug] No type-compatibility check between FK local column and referenced column — invalid FK slips through to save

1 participant