Skip to content

Commit

Permalink
Fix multiple foreign keys not being exported from generic to sqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
1ilit committed Aug 24, 2024
1 parent 728a092 commit 3cd0633
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 35 deletions.
19 changes: 2 additions & 17 deletions src/utils/exportSQL/generic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dbToTypes, defaultTypes } from "../../data/datatypes";
import { parseDefault } from "./shared";
import { getInlineFK, parseDefault } from "./shared";

export function getJsonType(f) {
if (!Object.keys(defaultTypes).includes(f.type)) {
Expand Down Expand Up @@ -325,21 +325,6 @@ export function getSQLiteType(field) {
}
}

export function getInlineFK(table, obj) {
let fk = "";
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()}`;
}
});
return fk;
}

export function jsonToSQLite(obj) {
return obj.tables
.map((table) => {
Expand Down Expand Up @@ -367,7 +352,7 @@ export function jsonToSQLite(obj) {
.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 Down
16 changes: 16 additions & 0 deletions src/utils/exportSQL/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,19 @@ export function exportFieldComment(comment) {
.map((commentLine) => `\t-- ${commentLine}\n`)
.join("");
}

export function getInlineFK(table, obj) {
let fks = [];
obj.references.forEach((r) => {
if (r.startTableId === table.id) {
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 fks.join(",\n");
}
20 changes: 2 additions & 18 deletions src/utils/exportSQL/sqlite.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exportFieldComment, parseDefault } from "./shared";
import { exportFieldComment, getInlineFK, parseDefault } from "./shared";

import { dbToTypes } from "../../data/datatypes";

Expand Down Expand Up @@ -41,20 +41,4 @@ export function toSqlite(diagram) {
.join("\n")}`;
})
.join("\n");
}

export function getInlineFK(table, obj) {
let fks = [];
obj.references.forEach((r) => {
if (r.startTableId === table.id) {
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 fks.join(",\n");
}
}

0 comments on commit 3cd0633

Please sign in to comment.