From c16063f14b5065550bfd641bf779b1662e5627ba Mon Sep 17 00:00:00 2001 From: rijkvanzanten Date: Thu, 27 May 2021 12:25:30 -0400 Subject: [PATCH] Fix o2m fetch when not grouping --- api/src/database/run-ast.ts | 13 ++++++++----- app/src/interfaces/list-o2m/list-o2m.vue | 8 +++++++- .../roles/item/components/permissions-overview.vue | 8 +++++++- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/api/src/database/run-ast.ts b/api/src/database/run-ast.ts index 43c45459dc..539d8159fe 100644 --- a/api/src/database/run-ast.ts +++ b/api/src/database/run-ast.ts @@ -58,7 +58,8 @@ export default async function runAST( const { columnsToSelect, primaryKeyField, nestedCollectionNodes } = await parseCurrentLevel( schema, collection, - children + children, + query ); // The actual knex query builder instance. This is a promise that resolves with the raw items from the db @@ -101,7 +102,8 @@ export default async function runAST( async function parseCurrentLevel( schema: SchemaOverview, collection: string, - children: (NestedCollectionNode | FieldNode)[] + children: (NestedCollectionNode | FieldNode)[], + query: Query ) { const primaryKeyField = schema.collections[collection].primary; const columnsInCollection = Object.keys(schema.collections[collection].fields); @@ -133,10 +135,11 @@ async function parseCurrentLevel( } /** - * Always fetch primary key in case there's a nested relation that needs it + * Always fetch primary key in case there's a nested relation that needs it, but only if there's + * no aggregation / grouping going on */ - const childrenContainRelational = children.some((child) => child.type !== 'field'); - if (childrenContainRelational && columnsToSelectInternal.includes(primaryKeyField) === false) { + const hasAggregationOrGrouping = 'aggregate' in query || 'group' in query; + if (columnsToSelectInternal.includes(primaryKeyField) === false && hasAggregationOrGrouping === false) { columnsToSelectInternal.push(primaryKeyField); } diff --git a/app/src/interfaces/list-o2m/list-o2m.vue b/app/src/interfaces/list-o2m/list-o2m.vue index c8d4f5b198..c12173687b 100644 --- a/app/src/interfaces/list-o2m/list-o2m.vue +++ b/app/src/interfaces/list-o2m/list-o2m.vue @@ -377,7 +377,13 @@ export default defineComponent({ const pkField = relatedPrimaryKeyField.value.field; const newValue = (props.value || []).map((item) => { - if (typeof item === 'object' && pkField in item && pkField in edits && item[pkField] === edits[pkField]) { + if ( + item && + typeof item === 'object' && + pkField in item && + pkField in edits && + item[pkField] === edits[pkField] + ) { return edits; } diff --git a/app/src/modules/settings/routes/roles/item/components/permissions-overview.vue b/app/src/modules/settings/routes/roles/item/components/permissions-overview.vue index fd3eac2036..61fa1c5650 100644 --- a/app/src/modules/settings/routes/roles/item/components/permissions-overview.vue +++ b/app/src/modules/settings/routes/roles/item/components/permissions-overview.vue @@ -71,6 +71,7 @@ import { Permission } from '@/types'; import api from '@/api'; import { appRecommendedPermissions, appMinimalPermissions } from '../../app-permissions'; import { unexpectedError } from '@/utils/unexpected-error'; +import { orderBy } from 'lodash'; export default defineComponent({ components: { PermissionsOverviewHeader, PermissionsOverviewRow }, @@ -97,7 +98,12 @@ export default defineComponent({ ); const systemCollections = computed(() => - collectionsStore.state.collections.filter((collection) => collection.collection.startsWith('directus_') === true) + orderBy( + collectionsStore.state.collections.filter( + (collection) => collection.collection.startsWith('directus_') === true + ), + 'name' + ) ); const systemVisible = ref(false);