Skip to content

fix: correct FK direction when dragging from parent to child table#1001

Open
20syldev wants to merge 1 commit intodrawdb-io:mainfrom
20syldev:fix/fk-cardinality-inversion
Open

fix: correct FK direction when dragging from parent to child table#1001
20syldev wants to merge 1 commit intodrawdb-io:mainfrom
20syldev:fix/fk-cardinality-inversion

Conversation

@20syldev
Copy link
Copy Markdown

Summary

When a user drags from a primary key field (the "one"/parent side) to a non-unique field (the "many"/child side), the relationship was stored with the PK table as startTable. Since all SQL exporters generate ALTER TABLE [startTable] ADD FOREIGN KEY(...), this produced incorrect SQL that placed the FK constraint on the parent (referenced) table instead of the child (FK holder) table.

Two complementary fixes:

Fix A — Normalize at creation time (Canvas.jsx): When getCardinality() returns ONE_TO_MANY (start field is unique/PK, end field is not), swap startTable/endTable and change cardinality to MANY_TO_ONE. This ensures startTable always represents the FK holder going forward.

Fix B — Safety net in exporters (shared.js + all SQL exporters): Added resolveFKDirection(r) helper that checks if a relationship has ONE_TO_MANY cardinality and swaps the FK/referenced table assignment accordingly. This handles any pre-existing diagrams saved before this fix.

Before

-- User drags from members.id (PK) → memberships.member_id
-- Wrong: FK placed on the referenced table
ALTER TABLE `members`
ADD FOREIGN KEY(`id`) REFERENCES `memberships`(`member_id`)

After

-- Correct: FK placed on the child table
ALTER TABLE `memberships`
ADD FOREIGN KEY(`member_id`) REFERENCES `members`(`id`)

Files changed

  • src/components/EditorCanvas/Canvas.jsx — normalize direction at creation time
  • src/utils/exportSQL/shared.js — add resolveFKDirection() helper
  • src/utils/exportSQL/mysql.js, mariadb.js, postgres.js, mssql.js, oraclesql.js — use resolveFKDirection()
  • src/utils/exportSQL/generic.js — all 5 FK sections updated

Test plan

  • Create a members (id PK) and memberships (member_id) table
  • Drag from members.id to memberships.member_id to create a 1:N relationship
  • Export to SQL — verify ALTER TABLE memberships ADD FOREIGN KEY(member_id) REFERENCES members(id)
  • Drag in reverse (from memberships.member_id to members.id) — verify same correct output
  • Load an old diagram with a ONE_TO_MANY relationship and verify export is now correct

Fixes #873

When a user drags from a primary key (one side) to a foreign key
field (many side), the relationship was stored with the PK table
as startTable. All SQL exports use startTable as the FK holder,
generating the wrong ALTER TABLE statement.

Fix A: normalize at creation time — when cardinality is ONE_TO_MANY,
swap start/end so startTable is always the FK holder (many side),
and set cardinality to MANY_TO_ONE.

Fix B: safety net in all SQL exporters — use resolveFKDirection()
to handle any pre-existing ONE_TO_MANY relationships in saved
diagrams.

Fixes drawdb-io#873
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 20, 2026

@20syldev is attempting to deploy a commit to the dottle's projects Team on Vercel.

A member of the Team first needs to authorize it.

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.

[BUG] SQL Export inverts Foreign Key cardinality (Visual vs SQL mismatch)

1 participant