Skip to content

Commit

Permalink
fix: has-many unlink api - accept array of object with primary key co…
Browse files Browse the repository at this point in the history
…lumn

Signed-off-by: Pranav C <pranavxc@gmail.com>
  • Loading branch information
pranavxc committed Dec 21, 2023
1 parent beabf15 commit 2f70779
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions packages/nocodb/src/db/BaseModelSqlv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5136,7 +5136,14 @@ class BaseModelSqlv2 {
if (childRows.length !== childIds.length) {
const missingIds = childIds.filter(
(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(
Expand Down Expand Up @@ -5175,9 +5182,28 @@ class BaseModelSqlv2 {
{
// validate Ids
{
const childRowsQb = this.dbDriver(childTn)
.select(childTable.primaryKey.column_name)
.whereIn(childTable.primaryKey.column_name, childIds);
const childRowsQb = this.dbDriver(childTn).select(
childTable.primaryKey.column_name,
);

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, {
raw: true,
Expand All @@ -5186,7 +5212,14 @@ class BaseModelSqlv2 {
if (childRows.length !== childIds.length) {
const missingIds = childIds.filter(
(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(
Expand Down

1 comment on commit 2f70779

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR changes have been deployed. Please run the following command to verify:

docker run -d -p 8888:8080 nocodb/nocodb-timely:0.202.10-pr-7274-20231221-1158

Please sign in to comment.