Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression in count database helper when counting filtered related fields #22448

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-trees-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

Check warning on line 1 in .changeset/plenty-trees-draw.md

View workflow job for this annotation

GitHub Actions / Lint

File ignored by default.
'@directus/api': patch
---

Fixed count function on relational, filtered fields
19 changes: 18 additions & 1 deletion api/src/database/helpers/fn/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Query, SchemaOverview } from '@directus/types';
import type { Knex } from 'knex';
import { applyFilter, generateAlias } from '../../../utils/apply-query.js';
import type { AliasMap } from '../../../utils/get-column-path.js';
import { DatabaseHelper } from '../types.js';

export type FnHelperOptions = {
Expand Down Expand Up @@ -41,6 +42,7 @@ export abstract class FnHelper extends DatabaseHelper {
throw new Error(`Field ${collectionName}.${column} isn't a nested relational collection`);
}

// generate a unique alias for the relation collection, to prevent collisions in self referencing relations
const alias = generateAlias();

let countQuery = this.knex
Expand All @@ -49,7 +51,22 @@ export abstract class FnHelper extends DatabaseHelper {
.where(this.knex.raw(`??.??`, [alias, relation.field]), '=', this.knex.raw(`??.??`, [table, currentPrimary]));

if (options?.query?.filter) {
countQuery = applyFilter(this.knex, this.schema, countQuery, options.query.filter, relation.collection, {}).query;
// set the newly aliased collection in the alias map as the default parent collection, indicated by '', for any nested filters
const aliasMap: AliasMap = {
'': {
alias,
collection: relation.collection,
},
};

countQuery = applyFilter(
this.knex,
this.schema,
countQuery,
options.query.filter,
relation.collection,
aliasMap,
).query;
}

return this.knex.raw('(' + countQuery.toQuery() + ')');
Expand Down
4 changes: 3 additions & 1 deletion api/src/utils/apply-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,9 @@ export function applyFilter(

validateFilterOperator(type, filterOperator, special);

applyFilterToQuery(`${collection}.${filterPath[0]}`, filterOperator, filterValue, logical);
const aliasedCollection = aliasMap['']?.alias || collection;

applyFilterToQuery(`${aliasedCollection}.${filterPath[0]}`, filterOperator, filterValue, logical, collection);
}
}

Expand Down