Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix: broken relations handle gracefully on ast and meta diff

mertmit42c00dcdb0ec
diff --git a/packages/nocodb/src/helpers/getAst.ts b/packages/nocodb/src/helpers/getAst.ts
index 2ac4c2eab1..ffbf40dc68 100644
--- a/packages/nocodb/src/helpers/getAst.ts
+++ b/packages/nocodb/src/helpers/getAst.ts
@@ -11,6 +11,7 @@ import {
UITypes,
ViewTypes,
} from 'nocodb-sdk';
+import { Logger } from '@nestjs/common';
import type { NcContext } from '~/interface/config';
import type { MetaService } from '~/meta/meta.service';
import type {
@@ -34,6 +35,8 @@ import { NcError } from '~/helpers/catchError';
import RowColorCondition from '~/models/RowColorCondition';
import Noco from '~/Noco';
+const logger = new Logger('getAst');
+
type Ast = {
[key: string]: 1 | true | null | Ast;
};
@@ -229,7 +232,9 @@ const getAst = async (
const columns = model.columns;
- const ast: Ast = await columns.reduce(async (obj, col: Column) => {
+ const ast: Ast = {};
+
+ for (const col of columns) {
let value: number | boolean | { [key: string]: any } = 1;
// TODO: also get from col.id
const nestedFields =
@@ -241,9 +246,19 @@ const getAst = async (
);
const model = await colOpt.getRelatedTable(context);
+ if (!model) {
+ // Skip this column - related table not found
+ // This allows data retrieval to continue even with broken relations
+ logger.warn(
+ `Skipping column ${col.title}: related table ${colOpt.fk_related_model_id} not found`,
+ );
+ ast[getFieldKey(col)] = null;
+ continue;
+ }
+
const { refContext: refTableContext } = colOpt.getRelContext(context);
- const { ast } = await getAst(refTableContext, {
+ const { ast: childAst } = await getAst(refTableContext, {
model,
query: query?.nested?.[col.title],
dependencyFields: (dependencyFields.nested[col.title] =
@@ -254,7 +269,7 @@ const getAst = async (
throwErrorIfInvalidParams,
});
- value = ast;
+ value = childAst;
// todo: include field relative to the relation => pk / fk
} else if (col.uidt === UITypes.Links) {
@@ -273,6 +288,16 @@ const getAst = async (
const model = await colOpt.getRelatedTable(context);
+ if (!model) {
+ // Skip this column - related table not found
+ // This allows data retrieval to continue even with broken relations
+ logger.warn(
+ `Skipping column ${col.title}: related table ${colOpt.fk_related_model_id} not found`,
+ );
+ ast[getFieldKey(col)] = null;
+ continue;
+ }
+
value = (
await getAst(refTableContext, {
model,
@@ -351,11 +376,8 @@ const getAst = async (
if (isRequested || col.pk)
await extractDependencies(context, col, dependencyFields);
- return {
- ...(await obj),
- [getFieldKey(col)]: isRequested,
- };
- }, Promise.resolve({}));
+ ast[getFieldKey(col)] = isRequested;
+ }
return { ast, dependencyFields, parsedQuery: dependencyFields };
};
diff --git a/packages/nocodb/src/services/datas.service.ts b/packages/nocodb/src/services/datas.service.ts
index 8bd27d998e..e6c18f7ee0 100644
--- a/packages/nocodb/src/services/datas.service.ts
+++ b/packages/nocodb/src/services/datas.service.ts
@@ -58,6 +58,7 @@ export class DatasService {
if (
!linkColumn ||
!isLinksOrLTAR(linkColumn) ||
+ !linkColumn.colOptions ||
linkColumn.colOptions.fk_related_model_id !== model.id
) {
NcError.get(context).fieldNotFound(param.query?.linkColumnId, {
@@ -65,7 +66,7 @@ export class DatasService {
});
}
- if (linkColumn.colOptions.fk_target_view_id) {
+ if (linkColumn.colOptions?.fk_target_view_id) {
view = await View.get(context, linkColumn.colOptions.fk_target_view_id);
}
}
diff --git a/packages/nocodb/src/services/meta-diffs.service.ts b/packages/nocodb/src/services/meta-diffs.service.ts
index 7214682917..3061095b71 100644
--- a/packages/nocodb/src/services/meta-diffs.service.ts
+++ b/packages/nocodb/src/services/meta-diffs.service.ts
@@ -340,13 +340,73 @@ export class MetaDiffsService {
);
const parentCol = await colOpt.getParentColumn(context);
const childCol = await colOpt.getChildColumn(context);
+
+ if (!parentCol || !childCol) {
+ // Parent or child column is missing - mark relation for removal
+ const ownerModel = await relationCol.getModel(context);
+ if (ownerModel) {
+ const ownerTable = changes.find(
+ (t) => t.table_name === ownerModel.table_name,
+ );
+ if (ownerTable) {
+ ownerTable.detectedChanges.push({
+ type: MetaDiffType.TABLE_RELATION_REMOVE,
+ msg: `Relation removed (${
+ !parentCol ? 'parent' : 'child'
+ } column missing)`,
+ colId: relationCol.id,
+ column: relationCol,
+ });
+ }
+ }
+ continue;
+ }
+
const parentModel = await parentCol.getModel(context);
const childModel = await childCol.getModel(context);
+ if (!parentModel || !childModel) {
+ // Parent or child model is missing - mark relation for removal
+ const ownerModel =
+ parentModel || childModel || (await relationCol.getModel(context));
+ if (ownerModel) {
+ const ownerTable = changes.find(
+ (t) => t.table_name === ownerModel.table_name,
+ );
+ if (ownerTable) {
+ ownerTable.detectedChanges.push({
+ type: MetaDiffType.TABLE_RELATION_REMOVE,
+ msg: `Relation removed (${
+ !parentModel ? 'parent' : 'child'
+ } table missing)`,
+ colId: relationCol.id,
+ column: relationCol,
+ });
+ }
+ }
+ continue;
+ }
+
// many to many relation
if (colOpt.type === RelationTypes.MANY_TO_MANY) {
const m2mModel = await colOpt.getMMModel(context);
+ if (!m2mModel) {
+ // M2M model is missing - mark relation for removal
+ const ownerTable = changes.find(
+ (t) => t.table_name === childModel.table_name,
+ );
+ if (ownerTable) {
+ ownerTable.detectedChanges.push({
+ type: MetaDiffType.TABLE_VIRTUAL_M2M_REMOVE,
+ msg: `Many to many removed (junction table missing)`,
+ colId: relationCol.id,
+ column: relationCol,
+ });
+ }
+ continue;
+ }
+
const relatedTable = tableList.find(
(t) => t.tn === parentModel.table_name,
);
@@ -1093,6 +1153,12 @@ export class MetaDiffsService {
normalColumns.length < 5 &&
assocModel.primaryKeys.length === 2
) {
+ // Ensure colOptions are populated
+ if (!belongsToCols[0].colOptions || !belongsToCols[1].colOptions) {
+ // Skip if colOptions are missing (corrupted data)
+ continue;
+ }
+
const modelA = await belongsToCols[0].colOptions.getRelatedTable(
context,
);
@@ -1100,6 +1166,11 @@ export class MetaDiffsService {
context,
);
+ if (!modelA || !modelB) {
+ // Skip if related models are missing (deleted or corrupted data)
+ continue;
+ }
+
await modelA.getColumns(context);
await modelB.getColumns(context);
@@ -1173,6 +1244,11 @@ export class MetaDiffsService {
const colOpt = await btCol.colOptions;
const model = await colOpt.getRelatedTable(context);
+ if (!model) {
+ // Skip if related model is missing
+ continue;
+ }
+
for (const col of await model.getColumns(context)) {
if (!isLinksOrLTAR(col.uidt)) continue;