From 277e18b73379e27dcb13aad7907d525203f5954d Mon Sep 17 00:00:00 2001 From: mertmit Date: Mon, 4 Dec 2023 10:23:30 +0300 Subject: [PATCH 1/2] fix: replace title with id for generateLookupSelectQuery Signed-off-by: mertmit --- .../src/db/generateLookupSelectQuery.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/nocodb/src/db/generateLookupSelectQuery.ts b/packages/nocodb/src/db/generateLookupSelectQuery.ts index 3ea73fac94..881ff3b0a2 100644 --- a/packages/nocodb/src/db/generateLookupSelectQuery.ts +++ b/packages/nocodb/src/db/generateLookupSelectQuery.ts @@ -300,7 +300,7 @@ export default async function generateLookupSelectQuery({ ( await lookupColumn.getColOptions() ).formula, - lookupColumn.title, + lookupColumn.id, model, lookupColumn, await model.getAliasColMapping(), @@ -323,7 +323,7 @@ export default async function generateLookupSelectQuery({ default: { selectQb.select( - `${prevAlias}.${lookupColumn.column_name} as ${lookupColumn.title}`, + `${prevAlias}.${lookupColumn.column_name} as ${lookupColumn.id}`, ); } @@ -343,14 +343,14 @@ export default async function generateLookupSelectQuery({ // alternate approach with array_agg return { builder: knex - .select(knex.raw('json_agg(??)::text', [lookupColumn.title])) + .select(knex.raw('json_agg(??)::text', [lookupColumn.id])) .from(selectQb.as(subQueryAlias)), }; /* // alternate approach with array_agg return { builder: knex - .select(knex.raw('array_agg(??)', [lookupColumn.title])) + .select(knex.raw('array_agg(??)', [lookupColumn.id])) .from(selectQb), };*/ // alternate approach with string aggregation @@ -358,7 +358,7 @@ export default async function generateLookupSelectQuery({ // builder: knex // .select( // knex.raw('STRING_AGG(??::text, ?)', [ - // lookupColumn.title, + // lookupColumn.id, // LOOKUP_VAL_SEPARATOR, // ]), // ) @@ -368,7 +368,7 @@ export default async function generateLookupSelectQuery({ return { builder: knex .select( - knex.raw('cast(JSON_ARRAYAGG(??) as NCHAR)', [lookupColumn.title]), + knex.raw('cast(JSON_ARRAYAGG(??) as NCHAR)', [lookupColumn.id]), ) .from(selectQb.as(subQueryAlias)), }; @@ -377,8 +377,8 @@ export default async function generateLookupSelectQuery({ // builder: knex // .select( // knex.raw('GROUP_CONCAT(?? ORDER BY ?? ASC SEPARATOR ?)', [ - // lookupColumn.title, - // lookupColumn.title, + // lookupColumn.id, + // lookupColumn.id, // LOOKUP_VAL_SEPARATOR, // ]), // ) @@ -386,12 +386,12 @@ export default async function generateLookupSelectQuery({ // }; } else if (baseModelSqlv2.isSqlite) { // ref: https://stackoverflow.com/questions/13382856/sqlite3-join-group-concat-using-distinct-with-custom-separator - // selectQb.orderBy(`${lookupColumn.title}`, 'asc'); + // selectQb.orderBy(`${lookupColumn.id}`, 'asc'); return { builder: knex .select( knex.raw(`group_concat(??, ?)`, [ - lookupColumn.title, + lookupColumn.id, LOOKUP_VAL_SEPARATOR, ]), ) @@ -399,12 +399,12 @@ export default async function generateLookupSelectQuery({ }; } else if (baseModelSqlv2.isMssql) { // ref: https://stackoverflow.com/questions/13382856/sqlite3-join-group-concat-using-distinct-with-custom-separator - // selectQb.orderBy(`${lookupColumn.title}`, 'asc'); + // selectQb.orderBy(`${lookupColumn.id}`, 'asc'); return { builder: knex .select( knex.raw(`STRING_AGG(??, ?)`, [ - lookupColumn.title, + lookupColumn.id, LOOKUP_VAL_SEPARATOR, ]), ) From 165fa45fc2b46b501f0c9c57acb8712772bdc45f Mon Sep 17 00:00:00 2001 From: mertmit Date: Mon, 4 Dec 2023 10:24:15 +0300 Subject: [PATCH 2/2] fix: group by has many bt part Signed-off-by: mertmit --- packages/nocodb/src/db/BaseModelSqlv2.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index 1e5175ea81..b49c80f4be 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -4457,10 +4457,24 @@ class BaseModelSqlv2 { (d) => d[col.id] && Object.keys(d[col.id]), ); if (btData) { - for (const k of Object.keys(btData[col.id])) { - const btAlias = idToAliasMap[k]; + if (typeof btData[col.id] === 'object') { + for (const k of Object.keys(btData[col.id])) { + const btAlias = idToAliasMap[k]; + if (!btAlias) { + idToAliasPromiseMap[k] = Column.get({ colId: k }).then( + (col) => { + return col.title; + }, + ); + } + } + } else { + // Has Many BT + const btAlias = idToAliasMap[col.id]; if (!btAlias) { - idToAliasPromiseMap[k] = Column.get({ colId: k }).then((col) => { + idToAliasPromiseMap[col.id] = Column.get({ + colId: col.id, + }).then((col) => { return col.title; }); } @@ -4480,7 +4494,7 @@ class BaseModelSqlv2 { const alias = idToAliasMap[key]; if (alias) { if (btMap[key]) { - if (value) { + if (value && typeof value === 'object') { const tempObj = {}; Object.entries(value).forEach(([k, v]) => { const btAlias = idToAliasMap[k];