Browse Source

Merge pull request #7146 from nocodb/fix/7144-bt-groupby

fix: group by has many bt part
fix/good-first
Raju Udava 12 months ago committed by GitHub
parent
commit
593aab8303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 24
      packages/nocodb/src/db/generateLookupSelectQuery.ts

18
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -4457,10 +4457,24 @@ class BaseModelSqlv2 {
(d) => d[col.id] && Object.keys(d[col.id]),
);
if (btData) {
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) => {
idToAliasPromiseMap[k] = Column.get({ colId: k }).then(
(col) => {
return col.title;
},
);
}
}
} else {
// Has Many BT
const btAlias = idToAliasMap[col.id];
if (!btAlias) {
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];

24
packages/nocodb/src/db/generateLookupSelectQuery.ts

@ -300,7 +300,7 @@ export default async function generateLookupSelectQuery({
(
await lookupColumn.getColOptions<FormulaColumn>()
).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,
]),
)

Loading…
Cancel
Save