Fix deep groupBy behavior for O2M relations (#23279)

* Add additional group field for o2m

* Add changeset

---------

Co-authored-by: daedalus <44623501+ComfortablyCoding@users.noreply.github.com>
This commit is contained in:
Hannes Küttner
2024-08-12 17:47:11 +02:00
committed by GitHub
parent 939423f257
commit ce626ab7ec
2 changed files with 19 additions and 3 deletions

View File

@@ -0,0 +1,6 @@
---
'@directus/api': patch
---
Fixed deep `groupBy` queries for O2M relations, where results were not correctly grouped under their respective parent
items

View File

@@ -4,7 +4,7 @@ import type { Knex } from 'knex';
import { isEmpty } from 'lodash-es';
import { fetchPermissions } from '../../../permissions/lib/fetch-permissions.js';
import { fetchPolicies } from '../../../permissions/lib/fetch-policies.js';
import type { FieldNode, FunctionFieldNode, NestedCollectionNode } from '../../../types/index.js';
import type { FieldNode, FunctionFieldNode, NestedCollectionNode, O2MNode } from '../../../types/index.js';
import { getRelationType } from '../../../utils/get-relation-type.js';
import { getDeepQuery } from '../utils/get-deep-query.js';
import { getRelatedCollection } from '../utils/get-related-collection.js';
@@ -252,8 +252,14 @@ export async function parseFields(
whenCase: [],
};
if (relationType === 'o2m' && !child!.query.sort) {
child!.query.sort = [relation.meta?.sort_field || context.schema.collections[relation.collection]!.primary];
if (isO2MNode(child) && !child.query.sort) {
child.query.sort = [relation.meta?.sort_field || context.schema.collections[relation.collection]!.primary];
}
if (isO2MNode(child) && child?.query.group && child.query.group[0] !== relation.field) {
// If a group by is used, the result needs to be grouped by the foreign key of the relation first, so results
// are correctly grouped under the foreign key when extracting the grouped results from the nested queries.
child.query.group.unshift(relation.field);
}
}
@@ -275,3 +281,7 @@ export async function parseFields(
return true;
});
}
export function isO2MNode(node: NestedCollectionNode | null): node is O2MNode {
return !!node && node.type === 'o2m';
}