diff --git a/packages/datalib/src/ChangeSet.ts b/packages/datalib/src/ChangeSet.ts index 4b6a2ddb..4c566cc6 100644 --- a/packages/datalib/src/ChangeSet.ts +++ b/packages/datalib/src/ChangeSet.ts @@ -333,6 +333,7 @@ export function getDeleteCascades(changeSet: ChangeSet, dbinfo: DatabaseInfo): C const allForeignKeys = _.flatten(dbinfo.tables.map(x => x.foreignKeys)); for (const baseCmd of changeSet.deletes) { const table = dbinfo.tables.find(x => x.pureName == baseCmd.pureName && x.schemaName == baseCmd.schemaName); + if (!table.primaryKey) continue; const dependencies = allForeignKeys.filter( x => x.refSchemaName == table.schemaName && x.refTableName == table.pureName ); @@ -385,7 +386,7 @@ export function getDeleteCascades(changeSet: ChangeSet, dbinfo: DatabaseInfo): C conditions: [ extractCondition(baseCmd, 't2'), // @ts-ignore - ...fk.columns.map(column => ({ + ...table.primaryKey.columns.map(column => ({ conditionType: 'binary', operator: '=', left: { @@ -395,7 +396,7 @@ export function getDeleteCascades(changeSet: ChangeSet, dbinfo: DatabaseInfo): C }, right: { exprType: 'column', - columnName: column.refColumnName, + columnName: column.columnName, source: { name: { pureName: fk.refTableName, @@ -419,6 +420,17 @@ export function getDeleteCascades(changeSet: ChangeSet, dbinfo: DatabaseInfo): C } resItem.commands.push(refCmd); } + + let resItem = res.find(x => x.title == baseCmd.pureName); + if (!resItem) { + resItem = { + title: baseCmd.pureName, + commands: [], + }; + res.push(resItem); + } + + resItem.commands.push(deleteToSql(baseCmd)); } return res; diff --git a/packages/web/src/modals/ConfirmSqlModal.svelte b/packages/web/src/modals/ConfirmSqlModal.svelte index fcdbbddb..a7bef44d 100644 --- a/packages/web/src/modals/ConfirmSqlModal.svelte +++ b/packages/web/src/modals/ConfirmSqlModal.svelte @@ -82,9 +82,16 @@ { + on:click={e => { closeCurrentModal(); - onConfirm(); + onConfirm( + e.detail.deleteReferencesCascade + ? deleteCascadesScripts + .filter(({ script, title }) => e.detail[`deleteReferences_${title}`] !== false) + .map(({ script, title }) => script) + .join('\n') + : null + ); }} />