fix: correct FK direction when dragging from parent to child table#1001
Open
20syldev wants to merge 1 commit intodrawdb-io:mainfrom
Open
fix: correct FK direction when dragging from parent to child table#100120syldev wants to merge 1 commit intodrawdb-io:mainfrom
20syldev wants to merge 1 commit intodrawdb-io:mainfrom
Conversation
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
|
@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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 generateALTER 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): WhengetCardinality()returnsONE_TO_MANY(start field is unique/PK, end field is not), swapstartTable/endTableand change cardinality toMANY_TO_ONE. This ensuresstartTablealways represents the FK holder going forward.Fix B — Safety net in exporters (
shared.js+ all SQL exporters): AddedresolveFKDirection(r)helper that checks if a relationship hasONE_TO_MANYcardinality and swaps the FK/referenced table assignment accordingly. This handles any pre-existing diagrams saved before this fix.Before
After
Files changed
src/components/EditorCanvas/Canvas.jsx— normalize direction at creation timesrc/utils/exportSQL/shared.js— addresolveFKDirection()helpersrc/utils/exportSQL/mysql.js,mariadb.js,postgres.js,mssql.js,oraclesql.js— useresolveFKDirection()src/utils/exportSQL/generic.js— all 5 FK sections updatedTest plan
members(id PK) andmemberships(member_id) tablemembers.idtomemberships.member_idto create a 1:N relationshipALTER TABLE memberships ADD FOREIGN KEY(member_id) REFERENCES members(id)memberships.member_idtomembers.id) — verify same correct outputONE_TO_MANYrelationship and verify export is now correctFixes #873