Skip to content

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

Fix invalid sql after swapping fields (#287)

1ilit317c3cc9bac6
diff --git a/src/components/EditorSidePanel/TablesTab/TableInfo.jsx b/src/components/EditorSidePanel/TablesTab/TableInfo.jsx
index a832132a..659669e5 100644
--- a/src/components/EditorSidePanel/TablesTab/TableInfo.jsx
+++ b/src/components/EditorSidePanel/TablesTab/TableInfo.jsx
@@ -14,11 +14,12 @@ import ColorPalette from "../../ColorPicker";
import TableField from "./TableField";
import IndexDetails from "./IndexDetails";
import { useTranslation } from "react-i18next";
+import { dbToTypes } from "../../../data/datatypes";
export default function TableInfo({ data }) {
const { t } = useTranslation();
const [indexActiveKey, setIndexActiveKey] = useState("");
- const { deleteTable, updateTable, updateField, setRelationships } =
+ const { deleteTable, updateTable, updateField, setRelationships, database } =
useDiagram();
const { setUndoStack, setRedoStack } = useUndoRedo();
const [editField, setEditField] = useState({});
@@ -106,8 +107,20 @@ export default function TableInfo({ data }) {
const a = data.fields[index];
const b = data.fields[j];
- updateField(data.id, index, { ...b, id: index });
- updateField(data.id, j, { ...a, id: j });
+ updateField(data.id, index, {
+ ...b,
+ ...(!dbToTypes[database][b.type].isSized && { size: "" }),
+ ...(!dbToTypes[database][b.type].hasCheck && { check: "" }),
+ ...(dbToTypes[database][b.type].noDefault && { default: "" }),
+ id: index,
+ });
+ updateField(data.id, j, {
+ ...a,
+ ...(!dbToTypes[database][a.type].isSized && { size: "" }),
+ ...(!dbToTypes[database][a.type].hasCheck && { check: "" }),
+ ...(!dbToTypes[database][a.type].noDefault && { default: "" }),
+ id: j,
+ });
setRelationships((prev) =>
prev.map((e) => {
diff --git a/src/utils/exportSQL/mariadb.js b/src/utils/exportSQL/mariadb.js
index b1cc3afd..aebf4ab8 100644
--- a/src/utils/exportSQL/mariadb.js
+++ b/src/utils/exportSQL/mariadb.js
@@ -1,6 +1,21 @@
import { parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes";
+import { DB } from "../../data/constants";
+
+function parseType(field) {
+ let res = field.type;
+
+ if (field.type === "SET" || field.type === "ENUM") {
+ res += `${field.values ? "(" + field.values.map((value) => "'" + value + "'").join(", ") + ")" : ""}`;
+ }
+
+ if (dbToTypes[DB.MARIADB][field.type].isSized) {
+ res += `${field.size && field.size !== "" ? "(" + field.size + ")" : ""}`;
+ }
+
+ return res;
+}
export function toMariaDB(diagram) {
return `${diagram.tables
@@ -9,9 +24,7 @@ export function toMariaDB(diagram) {
`CREATE OR REPLACE TABLE \`${table.name}\` (\n${table.fields
.map(
(field) =>
- `\t\`${
- field.name
- }\` ${field.type}${field.values ? "(" + field.values.map((value) => "'" + value + "'").join(", ") + ")" : ""}${field.unsigned ? " UNSIGNED" : ""}${field.notNull ? " NOT NULL" : ""}${
+ `\t\`${field.name}\` ${parseType(field)}${field.unsigned ? " UNSIGNED" : ""}${field.notNull ? " NOT NULL" : ""}${
field.increment ? " AUTO_INCREMENT" : ""
}${field.unique ? " UNIQUE" : ""}${
field.default !== ""
diff --git a/src/utils/exportSQL/mysql.js b/src/utils/exportSQL/mysql.js
index e1228d4f..28254aec 100644
--- a/src/utils/exportSQL/mysql.js
+++ b/src/utils/exportSQL/mysql.js
@@ -1,6 +1,21 @@
import { parseDefault } from "./shared";
import { dbToTypes } from "../../data/datatypes";
+import { DB } from "../../data/constants";
+
+function parseType(field) {
+ let res = field.type;
+
+ if (field.type === "SET" || field.type === "ENUM") {
+ res += `${field.values ? "(" + field.values.map((value) => "'" + value + "'").join(", ") + ")" : ""}`;
+ }
+
+ if (dbToTypes[DB.MYSQL][field.type].isSized) {
+ res += `${field.size && field.size !== "" ? "(" + field.size + ")" : ""}`;
+ }
+
+ return res;
+}
export function toMySQL(diagram) {
return `${diagram.tables
@@ -9,7 +24,7 @@ export function toMySQL(diagram) {
`CREATE TABLE \`${table.name}\` (\n${table.fields
.map(
(field) =>
- `\t\`${field.name}\` ${field.type}${field.values ? "(" + field.values.map((value) => "'" + value + "'").join(", ") + ")" : ""}${field.unsigned ? " UNSIGNED" : ""}${field.size !== undefined && field.size !== "" ? "(" + field.size + ")" : ""}${
+ `\t\`${field.name}\` ${parseType(field)}${field.unsigned ? " UNSIGNED" : ""}${
field.notNull ? " NOT NULL" : ""
}${
field.increment ? " AUTO_INCREMENT" : ""