Skip to content

Commit

Permalink
Fix multiple foreign keys not being exported in sqlite (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilit committed Aug 24, 2024
1 parent 1a1fa05 commit 728a092
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/utils/exportSQL/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function toSqlite(diagram) {
.map((f) => `"${f.name}"`)
.join(", ")})${inlineFK !== "" ? ",\n" : ""}`
: ""
}\t${inlineFK}\n);\n${table.indices
}${inlineFK}\n);\n${table.indices
.map(
(i) =>
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX IF NOT EXISTS "${
Expand All @@ -44,16 +44,17 @@ export function toSqlite(diagram) {
}

export function getInlineFK(table, obj) {
let fk = "";
let fks = [];
obj.references.forEach((r) => {
if (fk !== "") return;
if (r.startTableId === table.id) {
fk = `FOREIGN KEY ("${table.fields[r.startFieldId].name}") REFERENCES "${
obj.tables[r.endTableId].name
}"("${
obj.tables[r.endTableId].fields[r.endFieldId].name
}")\n\tON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()}`;
fks.push(
`\tFOREIGN KEY ("${table.fields[r.startFieldId].name}") REFERENCES "${
obj.tables[r.endTableId].name
}"("${
obj.tables[r.endTableId].fields[r.endFieldId].name
}")\n\tON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()}`,
);
}
});
return fk;
return fks.join(",\n");
}

0 comments on commit 728a092

Please sign in to comment.