Browse Source

fix: handle wrong nested relation param values

re #856

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/863/head
Pranav C 3 years ago
parent
commit
bd1816a34c
  1. 60
      packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSql.ts

60
packages/nocodb/src/lib/dataMapper/lib/sql/BaseModelSql.ts

@ -1266,7 +1266,10 @@ class BaseModelSql extends BaseModel {
); );
let { fields } = restArgs; let { fields } = restArgs;
const { cn } = this.hasManyRelations.find(({ tn }) => tn === child) || {}; const { cn } = this.hasManyRelations.find(({ tn }) => tn === child) || {};
const _cn = this.dbModels[child].columnToAlias?.[cn]; if (!this.dbModels[child]) {
return;
}
const _cn = this.dbModels[child]?.columnToAlias?.[cn];
if (fields !== '*' && fields.split(',').indexOf(cn) === -1) { if (fields !== '*' && fields.split(',').indexOf(cn) === -1) {
fields += ',' + cn; fields += ',' + cn;
@ -1345,6 +1348,10 @@ class BaseModelSql extends BaseModel {
fields += ',' + cn; fields += ',' + cn;
} }
if (!this.dbModels[child]) {
return;
}
const childs = await this._run( const childs = await this._run(
driver.union( driver.union(
parentIds.map(id => { parentIds.map(id => {
@ -1498,13 +1505,17 @@ class BaseModelSql extends BaseModel {
fields += ',' + this.pks[0].cn; fields += ',' + this.pks[0].cn;
} }
for (const parent of parents.split(',')) { if (parents)
const { cn } = for (const parent of parents.split(',')) {
this.belongsToRelations.find(({ rtn }) => rtn === parent) || {}; if (!parent) {
if (fields !== '*' && fields.split(',').indexOf(cn) === -1) { continue;
fields += ',' + cn; }
const { cn } =
this.belongsToRelations.find(({ rtn }) => rtn === parent) || {};
if (fields !== '*' && fields.split(',').indexOf(cn) === -1) {
fields += ',' + cn;
}
} }
}
const items = await this.list({ childs, where, fields, ...rest }); const items = await this.list({ childs, where, fields, ...rest });
@ -1525,22 +1536,23 @@ class BaseModelSql extends BaseModel {
); );
} }
await Promise.all( if (parents)
parents.split(',').map((parent, index): any => { await Promise.all(
if (!parent) { parents.split(',').map((parent, index): any => {
return; if (!parent) {
} return;
const { cn, rcn } = }
this.belongsToRelations.find(({ rtn }) => rtn === parent) || {}; const { cn, rcn } =
const parentIds = [ this.belongsToRelations.find(({ rtn }) => rtn === parent) || {};
...new Set(items.map(c => c[cn] || c[this.columnToAlias[cn]])) const parentIds = [
]; ...new Set(items.map(c => c[cn] || c[this.columnToAlias[cn]]))
return this._belongsTo( ];
{ parent, rcn, parentIds, childs: items, cn, ...rest }, return this._belongsTo(
index { parent, rcn, parentIds, childs: items, cn, ...rest },
); index
}) );
); })
);
if (items && items.length) { if (items && items.length) {
await Promise.all( await Promise.all(
@ -1945,6 +1957,8 @@ class BaseModelSql extends BaseModel {
fields += ',' + rcn; fields += ',' + rcn;
} }
if (!this.dbModels[parent]) return;
const parents = await this._run( const parents = await this._run(
driver(this.dbModels[parent].tnPath) driver(this.dbModels[parent].tnPath)
// .select(...fields.split(',') // .select(...fields.split(',')

Loading…
Cancel
Save