SqlTableDesigner fix

This commit is contained in:
Sedat ÖZTÜRK 2026-03-16 12:52:15 +03:00
parent 66a3261e2c
commit 5cc3dc5ab3
2 changed files with 35 additions and 19 deletions

View file

@ -282,7 +282,7 @@ const SqlObjectExplorer = ({
{loading && <div className="text-center py-8 text-gray-500 text-sm">{translate('::App.Platform.Loading')}</div>} {loading && <div className="text-center py-8 text-gray-500 text-sm">{translate('::App.Platform.Loading')}</div>}
{!loading && treeData.length === 0 && <div className="text-center py-8 text-gray-500 text-sm">{translate('::App.Platform.NoDataSourceSelected')}</div>} {!loading && treeData.length === 0 && <div className="text-center py-8 text-gray-500 text-sm">{translate('::App.Platform.NoDataSourceSelected')}</div>}
{!loading && filteredTree.length > 0 && <div className="p-1">{filteredTree.map((n) => renderNode(n))}</div>} {!loading && filteredTree.length > 0 && <div className="p-1">{filteredTree.map((n) => renderNode(n))}</div>}
{!loading && treeData.length > 0 && filteredTree.length === 0 && <div className="text-center py-8 text-gray-500 text-sm">{translate('::App.Platform.NoResultsFound')}</div>} {!loading && treeData.length > 0 && filteredTree.length === 0 && <div className="text-center py-8 text-gray-500 text-sm">{translate('::App.Platform.NoResults')}</div>}
</div> </div>
{/* Context menu */} {/* Context menu */}

View file

@ -257,14 +257,7 @@ function generateCreateTableSql(
const userCols = columns.filter((c) => c.columnName.trim()) const userCols = columns.filter((c) => c.columnName.trim())
const allBodyCols = userCols const allBodyCols = userCols
const bodyLines = allBodyCols.map((c, i) => colToSqlLine(c, i < allBodyCols.length - 1))
const hasIdCol = userCols.some((c) => c.columnName.trim().toLowerCase() === 'id')
const bodyLines = hasIdCol
? [
...allBodyCols.map((c) => colToSqlLine(c, true)),
` CONSTRAINT [PK_${tableName}] PRIMARY KEY NONCLUSTERED ([Id])`,
]
: allBodyCols.map((c, i) => colToSqlLine(c, i < allBodyCols.length - 1))
// FK constraint lines // FK constraint lines
const fkLines: string[] = [] const fkLines: string[] = []
@ -1000,22 +993,25 @@ const SqlTableDesignerDialog = ({
const nonEmpty = prev.filter((c) => c.columnName.trim() !== '') const nonEmpty = prev.filter((c) => c.columnName.trim() !== '')
return [...nonEmpty, ...toAdd.map((c) => ({ ...c })), createEmptyColumn()] return [...nonEmpty, ...toAdd.map((c) => ({ ...c })), createEmptyColumn()]
}) })
// Auto-add PK for Id column if not already defined
const hasPk = indexes.some((ix) => ix.indexType === 'PrimaryKey') // FullAudited ile Id eklendiğinde PK tanımı Index/Key listesine otomatik düşsün.
if (!hasPk) { setIndexes((prev) => {
const tblName = settings.tableName || initialTableData?.tableName || 'Table' const hasPk = prev.some((ix) => ix.indexType === 'PrimaryKey')
setIndexes((prev) => [ if (hasPk) return prev
const tableName = settings.tableName || initialTableData?.tableName || 'Table'
return [
...prev, ...prev,
{ {
id: `auto-pk-${crypto.randomUUID()}`, id: `auto-pk-${crypto.randomUUID()}`,
indexName: `PK_${tblName}`, indexName: `PK_${tableName}`,
indexType: 'PrimaryKey', indexType: 'PrimaryKey',
isClustered: false, isClustered: false,
columns: [{ columnName: 'Id', order: 'ASC' }], columns: [{ columnName: 'Id', order: 'ASC' }],
description: 'Primary key', description: 'Primary key (auto)',
}, },
]) ]
} })
} }
const addMultiTenantColumns = () => { const addMultiTenantColumns = () => {
@ -1051,8 +1047,11 @@ const SqlTableDesignerDialog = ({
// ── Settings helpers ─────────────────────────────────────────────────────── // ── Settings helpers ───────────────────────────────────────────────────────
const hasTenantIdColumn = (cols: ColumnDefinition[]) =>
cols.some((c) => c.columnName.trim().toLowerCase() === 'tenantid')
const buildTableName = (prefix: string, entity: string) => const buildTableName = (prefix: string, entity: string) =>
prefix && entity ? `${prefix}_D_${entity}` : '' prefix && entity ? `${prefix}_${hasTenantIdColumn(columns) ? 'T' : 'D'}_${entity}` : ''
const syncAutoPkName = (newTableName: string) => { const syncAutoPkName = (newTableName: string) => {
if (!newTableName) return if (!newTableName) return
@ -1093,6 +1092,23 @@ const SqlTableDesignerDialog = ({
syncAutoPkName(newTableName) syncAutoPkName(newTableName)
} }
useEffect(() => {
if (isEditMode) return
if (!settings.menuPrefix || !settings.entityName) return
const recalculatedTableName = buildTableName(settings.menuPrefix, settings.entityName)
if (!recalculatedTableName || recalculatedTableName === settings.tableName) return
setSettings((s) => ({ ...s, tableName: recalculatedTableName }))
syncAutoPkName(recalculatedTableName)
}, [
isEditMode,
columns,
settings.menuPrefix,
settings.entityName,
settings.tableName,
])
// ── FK Relationship handlers ─────────────────────────────────────────────── // ── FK Relationship handlers ───────────────────────────────────────────────
const loadTargetColumns = (tableName: string) => { const loadTargetColumns = (tableName: string) => {