Skip to content

Database diagrams editor that allows you to visualize and design your DB with a single query.

fix compatible fields check

Guy Ben-Aharon25ef1732897f
diff --git a/src/dialogs/create-relationship-dialog/create-relationship-dialog.tsx b/src/dialogs/create-relationship-dialog/create-relationship-dialog.tsx
index 68946a8e..14d46486 100644
--- a/src/dialogs/create-relationship-dialog/create-relationship-dialog.tsx
+++ b/src/dialogs/create-relationship-dialog/create-relationship-dialog.tsx
@@ -17,7 +17,7 @@ import { SelectBox } from '@/components/select-box/select-box';
import { useLayout } from '@/hooks/use-layout';
import { useReactFlow } from '@xyflow/react';
import type { BaseDialogProps } from '../common/base-dialog-props';
-import { areFieldTypesCompatible } from '@/lib/utils';
+import { areFieldTypesCompatible } from '@/lib/data/data-types';
const ErrorMessageRelationshipFieldsNotSameType =
'Relationships can only be created between fields of the same type';
diff --git a/src/i18n/locales/de.ts b/src/i18n/locales/de.ts
index 7c449d49..f97454a4 100644
--- a/src/i18n/locales/de.ts
+++ b/src/i18n/locales/de.ts
@@ -322,14 +322,6 @@ export const de: LanguageTranslation = {
edit_table: 'Tabelle bearbeiten',
delete_table: 'Tabelle löschen',
},
-
- field_type_change: {
- relationship_title: 'Zugehörige Felder ändern',
- relationship_description:
- 'Dieses Feld ist Teil einer oder mehrerer Beziehungen. Eine Änderung des Typs wirkt sich auf verwandte Felder in anderen Tabellen aus. Möchten Sie fortfahren?',
- change_all: 'Alles ändern',
- cancel: 'Abbrechen',
- },
},
};
diff --git a/src/i18n/locales/en.ts b/src/i18n/locales/en.ts
index 78c25be6..543d9e16 100644
--- a/src/i18n/locales/en.ts
+++ b/src/i18n/locales/en.ts
@@ -320,14 +320,6 @@ export const en = {
edit_table: 'Edit Table',
delete_table: 'Delete Table',
},
-
- field_type_change: {
- relationship_title: 'Change Related Fields',
- relationship_description:
- 'This field is part of one or more relationships. Changing its type will affect related fields in other tables. Do you want to proceed?',
- change_all: 'Change All',
- cancel: 'Cancel',
- },
},
};
diff --git a/src/i18n/locales/es.ts b/src/i18n/locales/es.ts
index c2c6f465..1f2b97e6 100644
--- a/src/i18n/locales/es.ts
+++ b/src/i18n/locales/es.ts
@@ -323,14 +323,6 @@ export const es: LanguageTranslation = {
edit_table: 'Editar Tabla',
delete_table: 'Eliminar Tabla',
},
-
- field_type_change: {
- relationship_title: 'Cambiar Campos Relacionados',
- relationship_description:
- 'Este campo es parte de una o más relaciones. Cambiar su tipo afectará a los campos relacionados en otras tablas. ¿Desea continuar?',
- change_all: 'Cambiar Todo',
- cancel: 'Cancelar',
- },
},
};
diff --git a/src/i18n/locales/fr.ts b/src/i18n/locales/fr.ts
index 08df80a5..770c836c 100644
--- a/src/i18n/locales/fr.ts
+++ b/src/i18n/locales/fr.ts
@@ -326,14 +326,6 @@ export const fr: LanguageTranslation = {
edit_table: 'Éditer la Table',
delete_table: 'Supprimer la Table',
},
-
- field_type_change: {
- relationship_title: 'Changer les Champs Relatifs',
- relationship_description:
- "Ce champ est partie d'une ou plusieurs relations. Un changement de type affectera les champs liés dans d'autres tables. Voulez-vous continuer ?",
- change_all: 'Changer Tout',
- cancel: 'Annuler',
- },
},
};
diff --git a/src/lib/data/data-types.ts b/src/lib/data/data-types.ts
index 56771128..8a408b17 100644
--- a/src/lib/data/data-types.ts
+++ b/src/lib/data/data-types.ts
@@ -321,3 +321,35 @@ export const dataTypeMap: Record<DatabaseType, readonly DataType[]> = {
[DatabaseType.MARIADB]: mariadbDataTypes,
[DatabaseType.SQLITE]: sqliteDataTypes,
} as const;
+
+const compatibleTypes: Record<DatabaseType, Record<string, string[]>> = {
+ [DatabaseType.POSTGRESQL]: {
+ serial: ['integer'],
+ smallserial: ['smallint'],
+ bigserial: ['bigint'],
+ },
+ [DatabaseType.MYSQL]: {
+ int: ['integer'],
+ tinyint: ['boolean'],
+ },
+ [DatabaseType.SQL_SERVER]: {},
+ [DatabaseType.MARIADB]: {},
+ [DatabaseType.SQLITE]: {},
+ [DatabaseType.GENERIC]: {},
+};
+
+export function areFieldTypesCompatible(
+ type1: DataType,
+ type2: DataType,
+ databaseType: DatabaseType
+): boolean {
+ if (type1.id === type2.id) {
+ return true;
+ }
+
+ const dbCompatibleTypes = compatibleTypes[databaseType] || {};
+ return (
+ dbCompatibleTypes[type1.id]?.includes(type2.id) ||
+ dbCompatibleTypes[type2.id]?.includes(type1.id)
+ );
+}
diff --git a/src/lib/domain/db-relationship.ts b/src/lib/domain/db-relationship.ts
index a5f18b06..b1a6ec86 100644
--- a/src/lib/domain/db-relationship.ts
+++ b/src/lib/domain/db-relationship.ts
@@ -156,15 +156,3 @@ export const determineCardinalities = (
return { sourceCardinality: 'many', targetCardinality: 'many' };
}
};
-
-export const getRelationshipsForField = (
- relationships: DBRelationship[],
- tableId: string,
- fieldId: string
-): DBRelationship[] => {
- return relationships.filter(
- (rel) =>
- (rel.sourceTableId === tableId && rel.sourceFieldId === fieldId) ||
- (rel.targetTableId === tableId && rel.targetFieldId === fieldId)
- );
-};
diff --git a/src/pages/editor-page/canvas/canvas.tsx b/src/pages/editor-page/canvas/canvas.tsx
index 5215c745..1582af8d 100644
--- a/src/pages/editor-page/canvas/canvas.tsx
+++ b/src/pages/editor-page/canvas/canvas.tsx
@@ -51,7 +51,7 @@ import {
import { useDialog } from '@/hooks/use-dialog';
import { MarkerDefinitions } from './marker-definitions';
import { CanvasContextMenu } from './canvas-context-menu';
-import { areFieldTypesCompatible } from '@/lib/utils';
+import { areFieldTypesCompatible } from '@/lib/data/data-types';
type AddEdgeParams = Parameters<typeof addEdge<TableEdgeType>>[0];
diff --git a/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-field/table-field.tsx b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-field/table-field.tsx
index 5aa10b80..edd351cb 100644
--- a/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-field/table-field.tsx
+++ b/src/pages/editor-page/side-panel/tables-section/table-list/table-list-item/table-list-item-content/table-field/table-field.tsx
@@ -25,7 +25,6 @@ import { TableFieldToggle } from './table-field-toggle';
import { useSortable } from '@dnd-kit/sortable';
import { CSS } from '@dnd-kit/utilities';
import { SelectBox } from '@/components/select-box/select-box';
-import { useDialog } from '@/hooks/use-dialog';
export interface TableFieldProps {
field: DBField;
@@ -53,55 +52,6 @@ export const TableField: React.FC<TableFieldProps> = ({
transition,
};
- const { showAlert } = useDialog();
- const { relationships, updateField: updateFieldGlobal } = useChartDB();
-
- const handleTypeChange = async (newTypeId: string) => {
- const newType = dataTypeMap[databaseType].find(
- (v) => v.id === newTypeId
- );
- if (!newType) return;
-
- const relatedRelationships = relationships.filter(
- (rel) =>
- rel.sourceFieldId === field.id || rel.targetFieldId === field.id
- );
-
- if (relatedRelationships.length > 0) {
- const confirmed = await new Promise<boolean>((resolve) => {
- showAlert({
- title: t('field_type_change.relationship_title'),
- description: t(
- 'field_type_change.relationship_description'
- ),
- actionLabel: t('field_type_change.change_all'),
- closeLabel: t('field_type_change.cancel'),
- onAction: () => resolve(true),
- onClose: () => resolve(false),
- });
- });
-
- if (!confirmed) return;
-
- // Update all related fields
- relatedRelationships.forEach((rel) => {
- const otherFieldId =
- rel.sourceFieldId === field.id
- ? rel.targetFieldId
- : rel.sourceFieldId;
- const otherFieldTableId =
- rel.sourceFieldId === field.id
- ? rel.targetTableId
- : rel.sourceTableId;
- updateFieldGlobal(otherFieldTableId, otherFieldId, {
- type: newType,
- });
- });
- }
-
- updateField({ type: newType });
- };
-
return (
<div
className="flex flex-1 touch-none flex-row justify-between p-1"
@@ -147,7 +97,11 @@ export const TableField: React.FC<TableFieldProps> = ({
)}
value={field.type.id}
onChange={(value) =>
- handleTypeChange(value as string)
+ updateField({
+ type: dataTypeMap[databaseType].find(
+ (v) => v.id === value
+ ),
+ })
}
emptyPlaceholder={t(
'side_panel.tables_section.table.no_types_found'