Skip to content

Commit 3cd0633

Browse files
committed
Fix multiple foreign keys not being exported from generic to sqlite
1 parent 728a092 commit 3cd0633

File tree

3 files changed

+20
-35
lines changed

3 files changed

+20
-35
lines changed

src/utils/exportSQL/generic.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dbToTypes, defaultTypes } from "../../data/datatypes";
2-
import { parseDefault } from "./shared";
2+
import { getInlineFK, parseDefault } from "./shared";
33

44
export function getJsonType(f) {
55
if (!Object.keys(defaultTypes).includes(f.type)) {
@@ -325,21 +325,6 @@ export function getSQLiteType(field) {
325325
}
326326
}
327327

328-
export function getInlineFK(table, obj) {
329-
let fk = "";
330-
obj.references.forEach((r) => {
331-
if (fk !== "") return;
332-
if (r.startTableId === table.id) {
333-
fk = `FOREIGN KEY ("${table.fields[r.startFieldId].name}") REFERENCES "${
334-
obj.tables[r.endTableId].name
335-
}"("${
336-
obj.tables[r.endTableId].fields[r.endFieldId].name
337-
}")\n\tON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()}`;
338-
}
339-
});
340-
return fk;
341-
}
342-
343328
export function jsonToSQLite(obj) {
344329
return obj.tables
345330
.map((table) => {
@@ -367,7 +352,7 @@ export function jsonToSQLite(obj) {
367352
.map((f) => `"${f.name}"`)
368353
.join(", ")})${inlineFK !== "" ? ",\n" : ""}`
369354
: ""
370-
}\t${inlineFK}\n);\n${table.indices
355+
}${inlineFK}\n);\n${table.indices
371356
.map(
372357
(i) =>
373358
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX IF NOT EXISTS "${

src/utils/exportSQL/shared.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,19 @@ export function exportFieldComment(comment) {
2626
.map((commentLine) => `\t-- ${commentLine}\n`)
2727
.join("");
2828
}
29+
30+
export function getInlineFK(table, obj) {
31+
let fks = [];
32+
obj.references.forEach((r) => {
33+
if (r.startTableId === table.id) {
34+
fks.push(
35+
`\tFOREIGN KEY ("${table.fields[r.startFieldId].name}") REFERENCES "${
36+
obj.tables[r.endTableId].name
37+
}"("${
38+
obj.tables[r.endTableId].fields[r.endFieldId].name
39+
}")\n\tON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()}`,
40+
);
41+
}
42+
});
43+
return fks.join(",\n");
44+
}

src/utils/exportSQL/sqlite.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { exportFieldComment, parseDefault } from "./shared";
1+
import { exportFieldComment, getInlineFK, parseDefault } from "./shared";
22

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

@@ -41,20 +41,4 @@ export function toSqlite(diagram) {
4141
.join("\n")}`;
4242
})
4343
.join("\n");
44-
}
45-
46-
export function getInlineFK(table, obj) {
47-
let fks = [];
48-
obj.references.forEach((r) => {
49-
if (r.startTableId === table.id) {
50-
fks.push(
51-
`\tFOREIGN KEY ("${table.fields[r.startFieldId].name}") REFERENCES "${
52-
obj.tables[r.endTableId].name
53-
}"("${
54-
obj.tables[r.endTableId].fields[r.endFieldId].name
55-
}")\n\tON UPDATE ${r.updateConstraint.toUpperCase()} ON DELETE ${r.deleteConstraint.toUpperCase()}`,
56-
);
57-
}
58-
});
59-
return fks.join(",\n");
60-
}
44+
}

0 commit comments

Comments
 (0)