From 3cd0633c01b03efcc44eedfa9bd3c54da2c09721 Mon Sep 17 00:00:00 2001 From: 1ilit <1ilit@proton.me> Date: Sat, 24 Aug 2024 23:53:10 +0400 Subject: [PATCH] Fix multiple foreign keys not being exported from generic to sqlite --- src/utils/exportSQL/generic.js | 19 ++----------------- src/utils/exportSQL/shared.js | 16 ++++++++++++++++ src/utils/exportSQL/sqlite.js | 20 ++------------------ 3 files changed, 20 insertions(+), 35 deletions(-) diff --git a/src/utils/exportSQL/generic.js b/src/utils/exportSQL/generic.js index c727487..cfb0922 100644 --- a/src/utils/exportSQL/generic.js +++ b/src/utils/exportSQL/generic.js @@ -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)) { @@ -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) => { @@ -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 "${ diff --git a/src/utils/exportSQL/shared.js b/src/utils/exportSQL/shared.js index c1df0c1..9e0f5cb 100644 --- a/src/utils/exportSQL/shared.js +++ b/src/utils/exportSQL/shared.js @@ -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"); +} diff --git a/src/utils/exportSQL/sqlite.js b/src/utils/exportSQL/sqlite.js index a63a356..25ae186 100644 --- a/src/utils/exportSQL/sqlite.js +++ b/src/utils/exportSQL/sqlite.js @@ -1,4 +1,4 @@ -import { exportFieldComment, parseDefault } from "./shared"; +import { exportFieldComment, getInlineFK, parseDefault } from "./shared"; import { dbToTypes } from "../../data/datatypes"; @@ -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"); -} +} \ No newline at end of file