Browse Source

fix(gui): use a unique alias in rollup query to avoid conflict

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5014/head
Pranav C 2 years ago
parent
commit
b132c33d31
  1. 16
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/genRollupSelectv2.ts

16
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/genRollupSelectv2.ts

@ -23,23 +23,21 @@ export default async function ({
const childModel = await childCol?.getModel(); const childModel = await childCol?.getModel();
const parentCol = await relationColumnOption.getParentColumn(); const parentCol = await relationColumnOption.getParentColumn();
const parentModel = await parentCol?.getModel(); const parentModel = await parentCol?.getModel();
const refTableAlias = `__nc_rollup`;
switch (relationColumnOption.type) { switch (relationColumnOption.type) {
case RelationTypes.HAS_MANY: case RelationTypes.HAS_MANY:
// if (!rollup.table_name || !rollup.rtn) {
// rollup = { ...rollup, ...hasMany.find(hm => hm.table_name === rollup.rltn) };
// }
return { return {
builder: knex(childModel?.table_name) builder: knex(`${childModel?.table_name} as ${refTableAlias}`)
[columnOptions.rollup_function]?.( [columnOptions.rollup_function]?.(
knex.ref(`${childModel?.table_name}.${rollupColumn.column_name}`) knex.ref(`${refTableAlias}.${rollupColumn.column_name}`)
) )
.where( .where(
knex.ref( knex.ref(
`${alias || parentModel.table_name}.${parentCol.column_name}` `${alias || parentModel.table_name}.${parentCol.column_name}`
), ),
'=', '=',
knex.ref(`${childModel.table_name}.${childCol.column_name}`) knex.ref(`${refTableAlias}.${childCol.column_name}`)
), ),
}; };
case RelationTypes.MANY_TO_MANY: { case RelationTypes.MANY_TO_MANY: {
@ -48,15 +46,15 @@ export default async function ({
const mmParentCol = await relationColumnOption.getMMParentColumn(); const mmParentCol = await relationColumnOption.getMMParentColumn();
return { return {
builder: knex(parentModel.table_name) builder: knex(`${parentModel?.table_name} as ${refTableAlias}`)
[columnOptions.rollup_function]?.( [columnOptions.rollup_function]?.(
knex.ref(`${parentModel.table_name}.${rollupColumn.column_name}`) knex.ref(`${refTableAlias}.${rollupColumn.column_name}`)
) )
.innerJoin( .innerJoin(
mmModel.table_name, mmModel.table_name,
knex.ref(`${mmModel.table_name}.${mmParentCol.column_name}`), knex.ref(`${mmModel.table_name}.${mmParentCol.column_name}`),
'=', '=',
knex.ref(`${parentModel.table_name}.${parentCol.column_name}`) knex.ref(`${refTableAlias}.${parentCol.column_name}`)
) )
.where( .where(
knex.ref(`${mmModel.table_name}.${mmChildCol.column_name}`), knex.ref(`${mmModel.table_name}.${mmChildCol.column_name}`),

Loading…
Cancel
Save