Browse Source

Merge pull request #1958 from nocodb/fix/m2m

fix: m2m delete logic
pull/1966/head
աɨռɢӄաօռɢ 3 years ago committed by GitHub
parent
commit
3428145e38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 53
      packages/nocodb/src/lib/noco/meta/api/columnApis.ts
  2. 2
      packages/nocodb/src/lib/noco/meta/api/tableApis.ts

53
packages/nocodb/src/lib/noco/meta/api/columnApis.ts

@ -725,11 +725,9 @@ export async function columnDelete(req: Request, res: Response<TableType>) {
}, },
true true
); );
const columnsInRelatedTable: Column[] = await relationColOpt const columnsInRelatedTable: Column[] = await relationColOpt
.getRelatedTable() .getRelatedTable()
.then(m => m.getColumns()); .then(m => m.getColumns());
let columnInRelatedTable: Column;
for (const c of columnsInRelatedTable) { for (const c of columnsInRelatedTable) {
if (c.uidt !== UITypes.LinkToAnotherRecord) continue; if (c.uidt !== UITypes.LinkToAnotherRecord) continue;
@ -737,26 +735,63 @@ export async function columnDelete(req: Request, res: Response<TableType>) {
LinkToAnotherRecordColumn LinkToAnotherRecordColumn
>(); >();
if ( if (
colOpt.type === 'mm' &&
colOpt.fk_parent_column_id === childColumn.id && colOpt.fk_parent_column_id === childColumn.id &&
colOpt.fk_child_column_id === parentColumn.id && colOpt.fk_child_column_id === parentColumn.id &&
colOpt.type === 'mm' &&
colOpt.fk_mm_model_id === mmTable.id && colOpt.fk_mm_model_id === mmTable.id &&
colOpt.fk_mm_parent_column_id === mmChildCol.id && colOpt.fk_mm_parent_column_id === mmChildCol.id &&
colOpt.fk_mm_child_column_id === mmParentCol.id colOpt.fk_mm_child_column_id === mmParentCol.id
) { ) {
columnInRelatedTable = c; await Column.delete(c.id);
break; break;
} }
} }
await Column.delete(relationColOpt.fk_column_id); await Column.delete(relationColOpt.fk_column_id);
await Column.delete(columnInRelatedTable.id);
// delete bt columns in m2m table
await mmTable.getColumns();
for (const c of mmTable.columns) {
if (c.uidt !== UITypes.LinkToAnotherRecord) continue;
const colOpt = await c.getColOptions<
LinkToAnotherRecordColumn
>();
if (colOpt.type === 'bt') {
await Column.delete(c.id);
}
}
// delete hm columns in parent table
await parentTable.getColumns();
for (const c of parentTable.columns) {
if (c.uidt !== UITypes.LinkToAnotherRecord) continue;
const colOpt = await c.getColOptions<
LinkToAnotherRecordColumn
>();
if (colOpt.fk_related_model_id === mmTable.id) {
await Column.delete(c.id);
}
}
// delete hm columns in child table
await childTable.getColumns();
for (const c of childTable.columns) {
if (c.uidt !== UITypes.LinkToAnotherRecord) continue;
const colOpt = await c.getColOptions<
LinkToAnotherRecordColumn
>();
if (colOpt.fk_related_model_id === mmTable.id) {
await Column.delete(c.id);
}
}
// retrieve columns in m2m table again
await mmTable.getColumns(); await mmTable.getColumns();
// ignore deleting table if it have more than 2 columns // ignore deleting table if it has more than 2 columns
// the expected 2 columns would be table1_id & table2_id
if (mmTable.columns.length === 2) { if (mmTable.columns.length === 2) {
await sqlMgr.sqlOpPlus(base, 'tableDelete', mmTable); await mmTable.delete();
} }
} }
break; break;
@ -867,7 +902,6 @@ const deleteHmOrBtRelation = async (
const columnsInRelatedTable: Column[] = await relationColOpt const columnsInRelatedTable: Column[] = await relationColOpt
.getRelatedTable() .getRelatedTable()
.then(m => m.getColumns()); .then(m => m.getColumns());
let columnInRelatedTable: Column;
const relType = relationColOpt.type === 'bt' ? 'hm' : 'bt'; const relType = relationColOpt.type === 'bt' ? 'hm' : 'bt';
for (const c of columnsInRelatedTable) { for (const c of columnsInRelatedTable) {
if (c.uidt !== UITypes.LinkToAnotherRecord) continue; if (c.uidt !== UITypes.LinkToAnotherRecord) continue;
@ -877,14 +911,13 @@ const deleteHmOrBtRelation = async (
colOpt.fk_child_column_id === childColumn.id && colOpt.fk_child_column_id === childColumn.id &&
colOpt.type === relType colOpt.type === relType
) { ) {
columnInRelatedTable = c; await Column.delete(c.id, ncMeta);
break; break;
} }
} }
// delete virtual columns // delete virtual columns
await Column.delete(relationColOpt.fk_column_id, ncMeta); await Column.delete(relationColOpt.fk_column_id, ncMeta);
await Column.delete(columnInRelatedTable.id, ncMeta);
if (!ignoreFkDelete) { if (!ignoreFkDelete) {
const cTable = await Model.getWithInfo({ const cTable = await Model.getWithInfo({

2
packages/nocodb/src/lib/noco/meta/api/tableApis.ts

@ -223,7 +223,7 @@ export async function tableDelete(req: Request, res: Response) {
) )
); );
NcError.badRequest( NcError.badRequest(
`Table can't be deleted since Table is being referred in following tables : ${referredTables.join( `Table can't be deleted since Table is being referred in following tables : ${referredTables.join(
', ' ', '
)}. Delete LinkToAnotherRecord columns and try again.` )}. Delete LinkToAnotherRecord columns and try again.`
); );

Loading…
Cancel
Save