Database diagrams editor that allows you to visualize and design your DB with a single query.
TypeScript databasedbdb-migrationeditorerdmariadbmssqlmysqlpostgresqlreactreact-flowschema-migrationssqlsqlitetypescriptvisualizationxyflow
fix pg support create a relation between compatible types
johnnyfish40e3d8da0a55
diff --git a/src/dialogs/create-relationship-dialog/create-relationship-dialog.tsx b/src/dialogs/create-relationship-dialog/create-relationship-dialog.tsxindex 086a6790..68946a8e 100644--- a/src/dialogs/create-relationship-dialog/create-relationship-dialog.tsx+++ b/src/dialogs/create-relationship-dialog/create-relationship-dialog.tsx@@ -17,6 +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';const ErrorMessageRelationshipFieldsNotSameType ='Relationships can only be created between fields of the same type';@@ -41,6 +42,7 @@ export const CreateRelationshipDialog: React.FC<const { openRelationshipFromSidebar } = useLayout();const [canCreateRelationship, setCanCreateRelationship] = useState(false);const { fitView, setEdges } = useReactFlow();+ const { databaseType } = useChartDB();const tableOptions = useMemo(() => {return tables.map(@@ -108,7 +110,13 @@ export const CreateRelationshipDialog: React.FC<return;}- if (primaryField.type.id !== referencedField.type.id) {+ if (+ !areFieldTypesCompatible(+ primaryField.type,+ referencedField.type,+ databaseType+ )+ ) {setErrorMessage(ErrorMessageRelationshipFieldsNotSameType);return;}@@ -121,6 +129,7 @@ export const CreateRelationshipDialog: React.FC<referencedFieldId,setErrorMessage,getField,+ databaseType,]);const handleCreateRelationship = useCallback(async () => {diff --git a/src/lib/utils.ts b/src/lib/utils.tsindex 9a0546d0..cdb4d068 100644--- a/src/lib/utils.ts+++ b/src/lib/utils.ts@@ -1,5 +1,39 @@import { type ClassValue, clsx } from 'clsx';import { customAlphabet } from 'nanoid';+import type { DataType } from './data/data-types';+import { DatabaseType } from './domain/database-type';++export function areFieldTypesCompatible(+ type1: DataType,+ type2: DataType,+ databaseType: DatabaseType+): boolean {+ // Check for exact match first+ if (type1.id === type2.id) {+ return true;+ }++ // Define compatible types for each database type+ const compatibleTypes: { [key: string]: { [key: string]: string[] } } = {+ [DatabaseType.POSTGRESQL]: {+ serial: ['integer'],+ smallserial: ['smallint'],+ bigserial: ['bigint'],+ },+ [DatabaseType.MYSQL]: {+ int: ['integer'],+ tinyint: ['boolean'],+ },+ // Add more database-specific compatibilities as needed+ };++ // Check if the types are compatible based on the database type+ const dbCompatibleTypes = compatibleTypes[databaseType] || {};+ return (+ dbCompatibleTypes[type1.id]?.includes(type2.id) ||+ dbCompatibleTypes[type2.id]?.includes(type1.id)+ );+}const randomId = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 25);diff --git a/src/pages/editor-page/canvas/canvas.tsx b/src/pages/editor-page/canvas/canvas.tsxindex 990ad0dd..5215c745 100644--- a/src/pages/editor-page/canvas/canvas.tsx+++ b/src/pages/editor-page/canvas/canvas.tsx@@ -51,6 +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';type AddEdgeParams = Parameters<typeof addEdge<TableEdgeType>>[0];@@ -90,6 +91,7 @@ export const Canvas: React.FC<CanvasProps> = ({ initialTables }) => {updateTablesState,removeRelationships,getField,+ databaseType,} = useChartDB();const { showSidePanel } = useLayout();const { effectiveTheme } = useTheme();@@ -222,12 +224,18 @@ export const Canvas: React.FC<CanvasProps> = ({ initialTables }) => {return;}- if (sourceField.type.id !== targetField.type.id) {+ if (+ !areFieldTypesCompatible(+ sourceField.type,+ targetField.type,+ databaseType+ )+ ) {toast({- title: 'Field types do not match',+ title: 'Field types are not compatible',variant: 'destructive',description:- 'Relationships can only be created between fields of the same type',+ 'Relationships can only be created between compatible field types',});return;}@@ -245,7 +253,7 @@ export const Canvas: React.FC<CanvasProps> = ({ initialTables }) => {// )// );},- [createRelationship, getField, toast]+ [createRelationship, getField, toast, databaseType]);const onEdgesChangeHandler: OnEdgesChange<TableEdgeType> = useCallback(