Skip to content

Free, simple, and intuitive online database diagram editor and SQL generator.

Fix self-referencing SQL imports (#1105)

Ngo Quoc Viet96edb66d1c4e
diff --git a/src/utils/importSQL/mariadb.js b/src/utils/importSQL/mariadb.js
index 6b4c2653..f7986fb5 100644
--- a/src/utils/importSQL/mariadb.js
+++ b/src/utils/importSQL/mariadb.js
@@ -1,7 +1,7 @@
import { nanoid } from "nanoid";
import { Cardinality, DB } from "../../data/constants";
import { dbToTypes } from "../../data/datatypes";
-import { buildSQLFromAST } from "./shared";
+import { buildSQLFromAST, findReferencedTable } from "./shared";
const affinity = {
[DB.MARIADB]: new Proxy(
@@ -119,7 +119,11 @@ export function fromMariaDB(ast, diagramDb = DB.GENERIC) {
);
const startFieldName = startFieldNames[0];
- const endTable = tables.find((t) => t.name === endTableName);
+ const endTable = findReferencedTable(
+ tables,
+ table,
+ endTableName,
+ );
if (!endTable) return;
const fieldPairs = [];
diff --git a/src/utils/importSQL/mysql.js b/src/utils/importSQL/mysql.js
index 84321cd6..6bf06f7f 100644
--- a/src/utils/importSQL/mysql.js
+++ b/src/utils/importSQL/mysql.js
@@ -1,7 +1,7 @@
import { nanoid } from "nanoid";
import { Cardinality, DB } from "../../data/constants";
import { dbToTypes } from "../../data/datatypes";
-import { buildSQLFromAST } from "./shared";
+import { buildSQLFromAST, findReferencedTable } from "./shared";
const affinity = {
[DB.MYSQL]: new Proxy(
@@ -118,7 +118,11 @@ export function fromMySQL(ast, diagramDb = DB.GENERIC) {
);
const startFieldName = startFieldNames[0];
- const endTable = tables.find((t) => t.name === endTableName);
+ const endTable = findReferencedTable(
+ tables,
+ table,
+ endTableName,
+ );
if (!endTable) return;
// Resolve every column pair (composite FKs carry more than one).
diff --git a/src/utils/importSQL/oraclesql.js b/src/utils/importSQL/oraclesql.js
index 33292277..4328d500 100644
--- a/src/utils/importSQL/oraclesql.js
+++ b/src/utils/importSQL/oraclesql.js
@@ -1,6 +1,7 @@
import { nanoid } from "nanoid";
import { Cardinality, Constraint, DB } from "../../data/constants";
import { dbToTypes } from "../../data/datatypes";
+import { findReferencedTable } from "./shared";
const affinity = {
[DB.ORACLESQL]: new Proxy(
@@ -105,7 +106,7 @@ export function fromOracleSQL(ast, diagramDb = DB.GENERIC) {
const startFieldName = startFieldNames[0];
const endTableName = d.constraint.reference.object.name;
- const endTable = tables.find((t) => t.name === endTableName);
+ const endTable = findReferencedTable(tables, table, endTableName);
if (!endTable) return;
const fieldPairs = [];
diff --git a/src/utils/importSQL/postgres.js b/src/utils/importSQL/postgres.js
index 397d6080..ddee8b90 100644
--- a/src/utils/importSQL/postgres.js
+++ b/src/utils/importSQL/postgres.js
@@ -1,7 +1,7 @@
import { nanoid } from "nanoid";
import { Cardinality, Constraint, DB } from "../../data/constants";
import { dbToTypes } from "../../data/datatypes";
-import { buildSQLFromAST } from "./shared";
+import { buildSQLFromAST, findReferencedTable } from "./shared";
const affinity = {
[DB.POSTGRES]: new Proxy(
@@ -139,7 +139,7 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
);
const startFieldName = startFieldNames[0];
- const endTable = tables.find((t) => t.name === endTableName);
+ const endTable = findReferencedTable(tables, table, endTableName);
if (!endTable) return;
const fieldPairs = [];
@@ -232,7 +232,7 @@ export function fromPostgres(ast, diagramDb = DB.GENERIC) {
}
});
- const endTable = tables.find((t) => t.name === endTableName);
+ const endTable = findReferencedTable(tables, table, endTableName);
if (!endTable) return;
const endField = endTable.fields.find(
diff --git a/src/utils/importSQL/shared.js b/src/utils/importSQL/shared.js
index cab0cbcf..521a22fb 100644
--- a/src/utils/importSQL/shared.js
+++ b/src/utils/importSQL/shared.js
@@ -1,5 +1,11 @@
import { DB } from "../../data/constants";
+export function findReferencedTable(tables, currentTable, name) {
+ return currentTable.name === name
+ ? currentTable
+ : tables.find((table) => table.name === name);
+}
+
function quoteColumn(str, db) {
switch (db) {
case DB.MYSQL:
diff --git a/src/utils/importSQL/sqlite.js b/src/utils/importSQL/sqlite.js
index da9d6587..51b4421a 100644
--- a/src/utils/importSQL/sqlite.js
+++ b/src/utils/importSQL/sqlite.js
@@ -1,7 +1,7 @@
import { nanoid } from "nanoid";
import { Cardinality, DB } from "../../data/constants";
import { dbToTypes } from "../../data/datatypes";
-import { buildSQLFromAST } from "./shared";
+import { buildSQLFromAST, findReferencedTable } from "./shared";
const affinity = {
[DB.SQLITE]: new Proxy(
@@ -51,7 +51,7 @@ export function fromSQLite(ast, diagramDb = DB.GENERIC) {
const endFieldNames = referenceDefinition.definition.map((c) => c.column);
const startFieldName = startFieldNames[0];
- const endTable = tables.find((t) => t.name === endTableName);
+ const endTable = findReferencedTable(tables, startTable, endTableName);
if (!endTable) return;
const fieldPairs = [];