Skip to content

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

fix column type change to support relationships related

johnnyfish784c7523872b
diff --git a/src/i18n/locales/de.ts b/src/i18n/locales/de.ts
index f97454a4..7c449d49 100644
--- a/src/i18n/locales/de.ts
+++ b/src/i18n/locales/de.ts
@@ -322,6 +322,14 @@ 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 543d9e16..78c25be6 100644
--- a/src/i18n/locales/en.ts
+++ b/src/i18n/locales/en.ts
@@ -320,6 +320,14 @@ 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 1f2b97e6..c2c6f465 100644
--- a/src/i18n/locales/es.ts
+++ b/src/i18n/locales/es.ts
@@ -323,6 +323,14 @@ 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 770c836c..08df80a5 100644
--- a/src/i18n/locales/fr.ts
+++ b/src/i18n/locales/fr.ts
@@ -326,6 +326,14 @@ 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/domain/db-relationship.ts b/src/lib/domain/db-relationship.ts
index b1a6ec86..a5f18b06 100644
--- a/src/lib/domain/db-relationship.ts
+++ b/src/lib/domain/db-relationship.ts
@@ -156,3 +156,15 @@ 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/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 edd351cb..5aa10b80 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,6 +25,7 @@ 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;
@@ -52,6 +53,55 @@ 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"
@@ -97,11 +147,7 @@ export const TableField: React.FC<TableFieldProps> = ({
)}
value={field.type.id}
onChange={(value) =>
- updateField({
- type: dataTypeMap[databaseType].find(
- (v) => v.id === value
- ),
- })
+ handleTypeChange(value as string)
}
emptyPlaceholder={t(
'side_panel.tables_section.table.no_types_found'