From 2b4eb320ed686b0a3135a7de9d8858dbf1cbf393 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Thu, 16 Nov 2023 12:24:12 +0000 Subject: [PATCH] fix: get aggregated value as json string array to avoid issues with separator --- .../src/db/generateLookupSelectQuery.ts | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/packages/nocodb/src/db/generateLookupSelectQuery.ts b/packages/nocodb/src/db/generateLookupSelectQuery.ts index 681e08a899..0cfb993d82 100644 --- a/packages/nocodb/src/db/generateLookupSelectQuery.ts +++ b/packages/nocodb/src/db/generateLookupSelectQuery.ts @@ -304,44 +304,52 @@ export default async function generateLookupSelectQuery({ const subQueryAlias = getAlias(); if (baseModelSqlv2.isPg) { - // selectQb.orderBy(`${lookupColumn.title}`, 'asc'); - // // alternate approach with array_agg - // return { - // builder: knex - // .select(knex.raw('array_agg(??)', [lookupColumn.title])) - // .from(selectQb), - // }; + selectQb.orderBy(`${lookupColumn.title}`, 'asc'); + // alternate approach with array_agg return { builder: knex - .select( - knex.raw('STRING_AGG(??::text, ?)', [ - lookupColumn.title, - LOOKUP_VAL_SEPARATOR, - ]), - ) - .from(selectQb.as(subQueryAlias)), + .select(knex.raw('json_agg(??)::text', [lookupColumn.title])) + .from(selectQb), }; - } else if (baseModelSqlv2.isMySQL) { - // // alternate approach with JSON_ARRAYAGG + /* + // alternate approach with array_agg + return { + builder: knex + .select(knex.raw('array_agg(??)', [lookupColumn.title])) + .from(selectQb), + };*/ + // alternate approach with string aggregation // return { // builder: knex // .select( - // knex.raw('cast(JSON_ARRAYAGG(??) as NCHAR)', [lookupColumn.title]), + // knex.raw('STRING_AGG(??::text, ?)', [ + // lookupColumn.title, + // LOOKUP_VAL_SEPARATOR, + // ]), // ) // .from(selectQb.as(subQueryAlias)), // }; - + } else if (baseModelSqlv2.isMySQL) { + // alternate approach with JSON_ARRAYAGG return { builder: knex .select( - knex.raw('GROUP_CONCAT(?? ORDER BY ?? ASC SEPARATOR ?)', [ - lookupColumn.title, - lookupColumn.title, - LOOKUP_VAL_SEPARATOR, - ]), + knex.raw('cast(JSON_ARRAYAGG(??) as NCHAR)', [lookupColumn.title]), ) .from(selectQb.as(subQueryAlias)), }; + + // return { + // builder: knex + // .select( + // knex.raw('GROUP_CONCAT(?? ORDER BY ?? ASC SEPARATOR ?)', [ + // lookupColumn.title, + // lookupColumn.title, + // LOOKUP_VAL_SEPARATOR, + // ]), + // ) + // .from(selectQb.as(subQueryAlias)), + // }; } else if (baseModelSqlv2.isSqlite) { // ref: https://stackoverflow.com/questions/13382856/sqlite3-join-group-concat-using-distinct-with-custom-separator // selectQb.orderBy(`${lookupColumn.title}`, 'asc');