Browse Source

fix: allow deletion of link columns with missing mm table

pull/7434/head
mertmit 11 months ago
parent
commit
438a9f554b
  1. 120
      packages/nocodb/src/services/columns.service.ts

120
packages/nocodb/src/services/columns.service.ts

@ -2179,9 +2179,11 @@ export class ColumnsService {
colOpt.type === 'mm' &&
colOpt.fk_parent_column_id === childColumn.id &&
colOpt.fk_child_column_id === parentColumn.id &&
colOpt.fk_mm_model_id === mmTable.id &&
colOpt.fk_mm_parent_column_id === mmChildCol.id &&
colOpt.fk_mm_child_column_id === mmParentCol.id
colOpt.fk_mm_model_id === relationColOpt.fk_mm_model_id &&
colOpt.fk_mm_parent_column_id ===
relationColOpt.fk_mm_child_column_id &&
colOpt.fk_mm_child_column_id ===
relationColOpt.fk_mm_parent_column_id
) {
await Column.delete(c.id, ncMeta);
break;
@ -2190,14 +2192,16 @@ export class ColumnsService {
await Column.delete(relationColOpt.fk_column_id, ncMeta);
// delete bt columns in m2m table
await mmTable.getColumns(ncMeta);
for (const c of mmTable.columns) {
if (!isLinksOrLTAR(c.uidt)) continue;
const colOpt =
await c.getColOptions<LinkToAnotherRecordColumn>(ncMeta);
if (colOpt.type === 'bt') {
await Column.delete(c.id, ncMeta);
if (mmTable) {
// delete bt columns in m2m table
await mmTable.getColumns(ncMeta);
for (const c of mmTable.columns) {
if (!isLinksOrLTAR(c.uidt)) continue;
const colOpt =
await c.getColOptions<LinkToAnotherRecordColumn>(ncMeta);
if (colOpt.type === 'bt') {
await Column.delete(c.id, ncMeta);
}
}
}
@ -2207,7 +2211,9 @@ export class ColumnsService {
if (!isLinksOrLTAR(c.uidt)) continue;
const colOpt =
await c.getColOptions<LinkToAnotherRecordColumn>(ncMeta);
if (colOpt.fk_related_model_id === mmTable.id) {
if (
colOpt.fk_related_model_id === relationColOpt.fk_mm_model_id
) {
await Column.delete(c.id, ncMeta);
}
}
@ -2218,20 +2224,24 @@ export class ColumnsService {
if (!isLinksOrLTAR(c.uidt)) continue;
const colOpt =
await c.getColOptions<LinkToAnotherRecordColumn>(ncMeta);
if (colOpt.fk_related_model_id === mmTable.id) {
if (
colOpt.fk_related_model_id === relationColOpt.fk_mm_model_id
) {
await Column.delete(c.id, ncMeta);
}
}
// retrieve columns in m2m table again
await mmTable.getColumns(ncMeta);
if (mmTable) {
// retrieve columns in m2m table again
await mmTable.getColumns(ncMeta);
// 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) {
(mmTable as any).tn = mmTable.table_name;
await sqlMgr.sqlOpPlus(source, 'tableDelete', mmTable);
await mmTable.delete(ncMeta);
// 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) {
(mmTable as any).tn = mmTable.table_name;
await sqlMgr.sqlOpPlus(source, 'tableDelete', mmTable);
await mmTable.delete(ncMeta);
}
}
}
break;
@ -2341,42 +2351,44 @@ export class ColumnsService {
},
ignoreFkDelete = false,
) => {
let foreignKeyName;
// if relationColOpt is not provided, extract it from child table
// and get the foreign key name for dropping the foreign key
if (!relationColOpt) {
foreignKeyName = (
(
await childTable.getColumns(ncMeta).then(async (cols) => {
for (const col of cols) {
if (col.uidt === UITypes.LinkToAnotherRecord) {
const colOptions =
await col.getColOptions<LinkToAnotherRecordColumn>(ncMeta);
if (colOptions.fk_related_model_id === parentTable.id) {
return { colOptions };
if (childTable) {
let foreignKeyName;
// if relationColOpt is not provided, extract it from child table
// and get the foreign key name for dropping the foreign key
if (!relationColOpt) {
foreignKeyName = (
(
await childTable.getColumns(ncMeta).then(async (cols) => {
for (const col of cols) {
if (col.uidt === UITypes.LinkToAnotherRecord) {
const colOptions =
await col.getColOptions<LinkToAnotherRecordColumn>(ncMeta);
if (colOptions.fk_related_model_id === parentTable.id) {
return { colOptions };
}
}
}
}
})
)?.colOptions as LinkToAnotherRecordType
).fk_index_name;
} else {
foreignKeyName = relationColOpt.fk_index_name;
}
})
)?.colOptions as LinkToAnotherRecordType
).fk_index_name;
} else {
foreignKeyName = relationColOpt.fk_index_name;
}
if (!relationColOpt?.virtual && !virtual) {
// todo: handle relation delete exception
try {
await sqlMgr.sqlOpPlus(source, 'relationDelete', {
childColumn: childColumn.column_name,
childTable: childTable.table_name,
parentTable: parentTable.table_name,
parentColumn: parentColumn.column_name,
foreignKeyName,
});
} catch (e) {
console.log(e.message);
if (!relationColOpt?.virtual && !virtual) {
// todo: handle relation delete exception
try {
await sqlMgr.sqlOpPlus(source, 'relationDelete', {
childColumn: childColumn.column_name,
childTable: childTable.table_name,
parentTable: parentTable.table_name,
parentColumn: parentColumn.column_name,
foreignKeyName,
});
} catch (e) {
console.log(e.message);
}
}
}

Loading…
Cancel
Save