Browse Source

fix: handle if fk is non-pk

pull/9051/head
Pranav C 4 months ago
parent
commit
ab0975ca66
  1. 29
      packages/nocodb/src/db/BaseModelSqlv2.ts

29
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -4320,10 +4320,25 @@ class BaseModelSqlv2 {
); );
insertObj[childCol.column_name] = nestedData?.[parentCol.title]; insertObj[childCol.column_name] = nestedData?.[parentCol.title];
} else { } else {
const parentCol = await colOptions.getParentColumn(
this.context,
);
const parentModel = await parentCol.getModel(this.context);
await parentModel.getColumns(this.context);
postInsertOps.push(async (rowId) => { 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)) return this.dbDriver(this.getTnPath(childModel.table_name))
.update({ .update({
[childCol.column_name]: rowId, [childCol.column_name]: refId,
}) })
.where( .where(
childModel.primaryKey.column_name, childModel.primaryKey.column_name,
@ -4338,13 +4353,23 @@ class BaseModelSqlv2 {
{ {
if (!Array.isArray(nestedData)) continue; if (!Array.isArray(nestedData)) continue;
const childCol = await colOptions.getChildColumn(this.context); const childCol = await colOptions.getChildColumn(this.context);
const parentCol = await colOptions.getParentColumn(this.context);
const childModel = await childCol.getModel(this.context); const childModel = await childCol.getModel(this.context);
const parentModel = await parentCol.getModel(this.context);
await childModel.getColumns(this.context); await childModel.getColumns(this.context);
await parentModel.getColumns(this.context);
postInsertOps.push(async (rowId) => { 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)) return this.dbDriver(this.getTnPath(childModel.table_name))
.update({ .update({
[childCol.column_name]: rowId, [childCol.column_name]: refId,
}) })
.whereIn( .whereIn(
childModel.primaryKey.column_name, childModel.primaryKey.column_name,

Loading…
Cancel
Save