From 9aae48d08964e3417d312e008e551e1c6f187fbf Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Thu, 28 May 2026 21:26:10 +0800 Subject: [PATCH] feat(structure): auto-switch column length default based on data type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When changing a column's data type in the table structure editor, the length parameter now updates to a sensible default for the new type (e.g. int→11, tinyint→4) instead of preserving the old value. --- .../structure/TableStructureEditor.vue | 3 +- .../src/lib/tableStructureEditorState.ts | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/apps/desktop/src/components/structure/TableStructureEditor.vue b/apps/desktop/src/components/structure/TableStructureEditor.vue index ad5c99dfe9..5de0068b73 100644 --- a/apps/desktop/src/components/structure/TableStructureEditor.vue +++ b/apps/desktop/src/components/structure/TableStructureEditor.vue @@ -42,6 +42,7 @@ import { createColumnDrafts, createIndexDrafts, DATA_TYPE_OPTIONS, + getDefaultLengthForType, splitDataType, toColumnNames, } from "@/lib/tableStructureEditorState"; @@ -574,7 +575,7 @@ watch( (column.dataType = combineDataTypeForDatabase( databaseType, v, - splitDataType(column.dataType).params, + getDefaultLengthForType(databaseType, v), )) " /> diff --git a/apps/desktop/src/lib/tableStructureEditorState.ts b/apps/desktop/src/lib/tableStructureEditorState.ts index bcfbf99a26..010f7ada36 100644 --- a/apps/desktop/src/lib/tableStructureEditorState.ts +++ b/apps/desktop/src/lib/tableStructureEditorState.ts @@ -230,6 +230,36 @@ export const DATA_TYPE_OPTIONS: Record = { ], }; +export const DEFAULT_TYPE_LENGTHS: Record = { + tinyint: "4", + smallint: "6", + mediumint: "9", + int: "11", + integer: "11", + int4: "11", + bigint: "20", + int8: "20", + float: "10,2", + real: "10,2", + "double precision": "10,2", + double: "10,2", + decimal: "10,0", + numeric: "10,0", + number: "10,0", + char: "1", + character: "1", + varchar: "255", + "character varying": "255", + varchar2: "255", + nvarchar2: "255", + nvarchar: "255", + nchar: "1", + varbinary: "255", + binary: "1", + bit: "1", + year: "4", +}; + export function createColumnDrafts(columns: ColumnInfo[]): EditableStructureColumn[] { return columns.map((column, index) => ({ id: `existing:${column.name}`, @@ -335,6 +365,11 @@ function isValidTemporalPrecision(dbType: DatabaseType | undefined, params: stri return Number.isInteger(value) && value >= 0 && value <= max && String(value) === params; } +export function getDefaultLengthForType(_dbType: DatabaseType | undefined, baseType: string): string { + const key = baseType.trim().toLowerCase(); + return DEFAULT_TYPE_LENGTHS[key] ?? ""; +} + export function buildStructureTargetLabel( connectionName: string | undefined, database: string | undefined,