From ab0975ca66f0b4bbe680616752365fa2539f5e51 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 22 Jul 2024 18:53:19 +0000 Subject: [PATCH] fix: handle if fk is non-pk --- packages/nocodb/src/db/BaseModelSqlv2.ts | 29 ++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index 5fa2f16288..308719c0a7 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -4320,10 +4320,25 @@ class BaseModelSqlv2 { ); insertObj[childCol.column_name] = nestedData?.[parentCol.title]; } else { + const parentCol = await colOptions.getParentColumn( + this.context, + ); + const parentModel = await parentCol.getModel(this.context); + await parentModel.getColumns(this.context); + postInsertOps.push(async (rowId) => { + let refId = rowId; + if (parentModel.primaryKey.id !== parentCol.id) { + refId = this.dbDriver( + this.getTnPath(parentModel.table_name), + ) + .select(parentCol.column_name) + .where(parentModel.primaryKey.column_name, rowId) + .first(); + } return this.dbDriver(this.getTnPath(childModel.table_name)) .update({ - [childCol.column_name]: rowId, + [childCol.column_name]: refId, }) .where( childModel.primaryKey.column_name, @@ -4338,13 +4353,23 @@ class BaseModelSqlv2 { { if (!Array.isArray(nestedData)) continue; const childCol = await colOptions.getChildColumn(this.context); + const parentCol = await colOptions.getParentColumn(this.context); const childModel = await childCol.getModel(this.context); + const parentModel = await parentCol.getModel(this.context); await childModel.getColumns(this.context); + await parentModel.getColumns(this.context); postInsertOps.push(async (rowId) => { + let refId = rowId; + if (parentModel.primaryKey.id !== parentCol.id) { + refId = this.dbDriver(this.getTnPath(parentModel.table_name)) + .select(parentCol.column_name) + .where(parentModel.primaryKey.column_name, rowId) + .first(); + } return this.dbDriver(this.getTnPath(childModel.table_name)) .update({ - [childCol.column_name]: rowId, + [childCol.column_name]: refId, }) .whereIn( childModel.primaryKey.column_name,