Fix multiple foreign keys not being exported from generic to sqlite

This commit is contained in:
1ilit 2024-08-24 23:53:10 +04:00
parent 728a092ac6
commit 3cd0633c01
3 changed files with 20 additions and 35 deletions

View File

@ -1,5 +1,5 @@
import { dbToTypes, defaultTypes } from "../../data/datatypes"; import { dbToTypes, defaultTypes } from "../../data/datatypes";
import { parseDefault } from "./shared"; import { getInlineFK, parseDefault } from "./shared";
export function getJsonType(f) { export function getJsonType(f) {
if (!Object.keys(defaultTypes).includes(f.type)) { 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) { export function jsonToSQLite(obj) {
return obj.tables return obj.tables
.map((table) => { .map((table) => {
@ -367,7 +352,7 @@ export function jsonToSQLite(obj) {
.map((f) => `"${f.name}"`) .map((f) => `"${f.name}"`)
.join(", ")})${inlineFK !== "" ? ",\n" : ""}` .join(", ")})${inlineFK !== "" ? ",\n" : ""}`
: "" : ""
}\t${inlineFK}\n);\n${table.indices }${inlineFK}\n);\n${table.indices
.map( .map(
(i) => (i) =>
`\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX IF NOT EXISTS "${ `\nCREATE ${i.unique ? "UNIQUE " : ""}INDEX IF NOT EXISTS "${

View File

@ -26,3 +26,19 @@ export function exportFieldComment(comment) {
.map((commentLine) => `\t-- ${commentLine}\n`) .map((commentLine) => `\t-- ${commentLine}\n`)
.join(""); .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");
}

View File

@ -1,4 +1,4 @@
import { exportFieldComment, parseDefault } from "./shared"; import { exportFieldComment, getInlineFK, parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes"; import { dbToTypes } from "../../data/datatypes";
@ -41,20 +41,4 @@ export function toSqlite(diagram) {
.join("\n")}`; .join("\n")}`;
}) })
.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");
}