Browse Source

fix: delete custom relation on junction table delete/ fk column delete

pull/8367/head
Pranav C 5 months ago
parent
commit
4843a60ade
  1. 16
      packages/nocodb/src/models/Column.ts
  2. 52
      packages/nocodb/src/services/tables.service.ts

16
packages/nocodb/src/models/Column.ts

@ -692,6 +692,22 @@ export default class Column<T = any> implements ColumnType {
}, },
}, },
); );
// if custom relation then delete
if (
links.every((lk) => {
try {
return parseMetaProp(lk)?.custom;
} catch {
// ignore
}
})
) {
for (const link of links) {
await Column.delete(link.id, ncMeta);
}
}
if (links.length) { if (links.length) {
NcError.columnAssociatedWithLink(id); NcError.columnAssociatedWithLink(id);
} }

52
packages/nocodb/src/services/tables.service.ts

@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common'; import {Injectable} from '@nestjs/common';
import DOMPurify from 'isomorphic-dompurify'; import DOMPurify from 'isomorphic-dompurify';
import { import {
AppEvents, AppEvents,
@ -11,28 +11,28 @@ import {
RelationTypes, RelationTypes,
UITypes, UITypes,
} from 'nocodb-sdk'; } from 'nocodb-sdk';
import { MetaDiffsService } from './meta-diffs.service'; import {MetaDiffsService} from './meta-diffs.service';
import { ColumnsService } from './columns.service'; import {ColumnsService} from './columns.service';
import type { import type {
ColumnType, ColumnType,
NormalColumnRequestType, NormalColumnRequestType,
TableReqType, TableReqType,
UserType, UserType,
} from 'nocodb-sdk'; } from 'nocodb-sdk';
import type { MetaService } from '~/meta/meta.service'; import type {MetaService} from '~/meta/meta.service';
import type { LinkToAnotherRecordColumn, User, View } from '~/models'; import type {LinkToAnotherRecordColumn, User, View} from '~/models';
import type { NcContext, NcRequest } from '~/interface/config'; import type { NcContext, NcRequest } from '~/interface/config';
import { Base, Column, Model, ModelRoleVisibility } from '~/models'; import {Base, Column, Model, ModelRoleVisibility} from '~/models';
import { AppHooksService } from '~/services/app-hooks/app-hooks.service'; import {AppHooksService} from '~/services/app-hooks/app-hooks.service';
import ProjectMgrv2 from '~/db/sql-mgr/v2/ProjectMgrv2'; import ProjectMgrv2 from '~/db/sql-mgr/v2/ProjectMgrv2';
import { NcError } from '~/helpers/catchError'; import {NcError} from '~/helpers/catchError';
import getColumnPropsFromUIDT from '~/helpers/getColumnPropsFromUIDT'; import getColumnPropsFromUIDT from '~/helpers/getColumnPropsFromUIDT';
import getColumnUiType from '~/helpers/getColumnUiType'; import getColumnUiType from '~/helpers/getColumnUiType';
import getTableNameAlias, { getColumnNameAlias } from '~/helpers/getTableName'; import getTableNameAlias, {getColumnNameAlias} from '~/helpers/getTableName';
import mapDefaultDisplayValue from '~/helpers/mapDefaultDisplayValue'; import mapDefaultDisplayValue from '~/helpers/mapDefaultDisplayValue';
import Noco from '~/Noco'; import Noco from '~/Noco';
import NcConnectionMgrv2 from '~/utils/common/NcConnectionMgrv2'; import NcConnectionMgrv2 from '~/utils/common/NcConnectionMgrv2';
import { sanitizeColumnName, validatePayload } from '~/helpers'; import {sanitizeColumnName, validatePayload} from '~/helpers';
import { import {
getUniqueColumnAliasName, getUniqueColumnAliasName,
getUniqueColumnName, getUniqueColumnName,
@ -44,7 +44,8 @@ export class TablesService {
protected readonly metaDiffService: MetaDiffsService, protected readonly metaDiffService: MetaDiffsService,
protected readonly appHooksService: AppHooksService, protected readonly appHooksService: AppHooksService,
protected readonly columnsService: ColumnsService, protected readonly columnsService: ColumnsService,
) {} ) {
}
async tableUpdate( async tableUpdate(
context: NcContext, context: NcContext,
@ -212,9 +213,9 @@ export class TablesService {
return ( return (
isLinksOrLTAR(c) && isLinksOrLTAR(c) &&
(c.colOptions as LinkToAnotherRecordColumn).type === (c.colOptions as LinkToAnotherRecordColumn).type ===
RelationTypes.MANY_TO_MANY && RelationTypes.MANY_TO_MANY &&
(c.colOptions as LinkToAnotherRecordColumn).fk_mm_model_id === (c.colOptions as LinkToAnotherRecordColumn).fk_mm_model_id ===
table.id table.id
); );
}); });
}); });
@ -226,6 +227,19 @@ export class TablesService {
); );
} }
// if table is using in custom relation as junction table then delete all the relation
const relations = await Noco.ncMeta.metaList2(null, null, MetaTable.COL_RELATIONS, {
condition: {
fk_mm_model_id: table.id,
},
});
if (relations?.data?.list?.length) {
for (const relation of relations.data.list) {
await Column.delete(relation.fk_column_id);
}
}
const base = await Base.getWithInfo(context, table.base_id); const base = await Base.getWithInfo(context, table.base_id);
const source = base.sources.find((b) => b.id === table.source_id); const source = base.sources.find((b) => b.id === table.source_id);
@ -356,7 +370,7 @@ export class TablesService {
'guest', 'guest',
]; ];
const defaultDisabled = roles.reduce((o, r) => ({ ...o, [r]: false }), {}); const defaultDisabled = roles.reduce((o, r) => ({...o, [r]: false}), {});
let models = let models =
_models || _models ||
@ -380,7 +394,7 @@ export class TablesService {
_tn: view.title, _tn: view.title,
table_meta: model.meta, table_meta: model.meta,
...view, ...view,
disabled: { ...defaultDisabled }, disabled: {...defaultDisabled},
}; };
} }
@ -503,7 +517,7 @@ export class TablesService {
if (!col || !col.system) { if (!col || !col.system) {
tableCreatePayLoad.columns.push({ tableCreatePayLoad.columns.push({
...(await getColumnPropsFromUIDT({ uidt } as any, source)), ...(await getColumnPropsFromUIDT({uidt} as any, source)),
column_name: colName, column_name: colName,
cn: colName, cn: colName,
title: colAlias, title: colAlias,
@ -681,9 +695,9 @@ export class TablesService {
let columns: Array< let columns: Array<
Omit<Column, 'column_name' | 'title'> & { Omit<Column, 'column_name' | 'title'> & {
cn: string; cn: string;
system?: boolean; system?: boolean;
} }
>; >;
if (!source.isMeta()) { if (!source.isMeta()) {

Loading…
Cancel
Save