Browse Source

Merge pull request #7274 from nocodb/fix/unlink-api-correction

fix: Has-many unlink api - accept array of object with primary key
pull/7273/head
Raju Udava 11 months ago committed by GitHub
parent
commit
785ad495a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 43
      packages/nocodb/src/db/BaseModelSqlv2.ts

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

@ -5136,7 +5136,14 @@ class BaseModelSqlv2 {
if (childRows.length !== childIds.length) { if (childRows.length !== childIds.length) {
const missingIds = childIds.filter( const missingIds = childIds.filter(
(id) => (id) =>
!childRows.find((r) => r[parentColumn.column_name] === id), !childRows.find(
(r) =>
r[parentColumn.column_name] ===
(typeof id === 'object'
? id[parentTable.primaryKey.title] ||
id[parentTable.primaryKey.column_name]
: id),
),
); );
NcError.unprocessableEntity( NcError.unprocessableEntity(
@ -5175,9 +5182,28 @@ class BaseModelSqlv2 {
{ {
// validate Ids // validate Ids
{ {
const childRowsQb = this.dbDriver(childTn) const childRowsQb = this.dbDriver(childTn).select(
.select(childTable.primaryKey.column_name) childTable.primaryKey.column_name,
.whereIn(childTable.primaryKey.column_name, childIds); );
if (parentTable.primaryKeys.length > 1) {
childRowsQb.where((qb) => {
for (const childId of childIds) {
qb.orWhere(_wherePk(parentTable.primaryKeys, childId));
}
});
} else if (typeof childIds[0] === 'object') {
childRowsQb.whereIn(
parentTable.primaryKey.column_name,
childIds.map(
(c) =>
c[parentTable.primaryKey.title] ||
c[parentTable.primaryKey.column_name],
),
);
} else {
childRowsQb.whereIn(parentTable.primaryKey.column_name, childIds);
}
const childRows = await this.execAndParse(childRowsQb, null, { const childRows = await this.execAndParse(childRowsQb, null, {
raw: true, raw: true,
@ -5186,7 +5212,14 @@ class BaseModelSqlv2 {
if (childRows.length !== childIds.length) { if (childRows.length !== childIds.length) {
const missingIds = childIds.filter( const missingIds = childIds.filter(
(id) => (id) =>
!childRows.find((r) => r[parentColumn.column_name] === id), !childRows.find(
(r) =>
r[parentColumn.column_name] ===
(typeof id === 'object'
? id[parentTable.primaryKey.title] ||
id[parentTable.primaryKey.column_name]
: id),
),
); );
NcError.unprocessableEntity( NcError.unprocessableEntity(

Loading…
Cancel
Save