mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Fix aliased DB table names in filter query (#13575)
* Fix aliased DB table names in filter query * Rename to originalCollectionName for clarity * Update api/src/utils/apply-query.ts Co-authored-by: Azri Kahar <42867097+azrikahar@users.noreply.github.com>
This commit is contained in:
@@ -357,7 +357,13 @@ export function applyFilter(
|
||||
|
||||
if (!columnName) continue;
|
||||
|
||||
applyFilterToQuery(columnName, filterOperator, filterValue, logical);
|
||||
if (relation?.related_collection) {
|
||||
applyFilterToQuery(columnName, filterOperator, filterValue, logical, relation.related_collection); // m2o
|
||||
} else if (filterPath[0].includes(':')) {
|
||||
applyFilterToQuery(columnName, filterOperator, filterValue, logical, filterPath[0].split(':')[1]); // a2o
|
||||
} else {
|
||||
applyFilterToQuery(columnName, filterOperator, filterValue, logical);
|
||||
}
|
||||
} else {
|
||||
applyFilterToQuery(`${collection}.${filterPath[0]}`, filterOperator, filterValue, logical);
|
||||
}
|
||||
@@ -394,7 +400,13 @@ export function applyFilter(
|
||||
}
|
||||
}
|
||||
}
|
||||
function applyFilterToQuery(key: string, operator: string, compareValue: any, logical: 'and' | 'or' = 'and') {
|
||||
function applyFilterToQuery(
|
||||
key: string,
|
||||
operator: string,
|
||||
compareValue: any,
|
||||
logical: 'and' | 'or' = 'and',
|
||||
originalCollectionName?: string
|
||||
) {
|
||||
const [table, column] = key.split('.');
|
||||
|
||||
// Is processed through Knex.Raw, so should be safe to string-inject into these where queries
|
||||
@@ -436,9 +448,10 @@ export function applyFilter(
|
||||
|
||||
// Cast filter value (compareValue) based on type of field being filtered against
|
||||
const [collection, field] = key.split('.');
|
||||
const mappedCollection = originalCollectionName || collection;
|
||||
|
||||
if (collection in schema.collections && field in schema.collections[collection].fields) {
|
||||
const type = schema.collections[collection].fields[field].type;
|
||||
if (mappedCollection in schema.collections && field in schema.collections[mappedCollection].fields) {
|
||||
const type = schema.collections[mappedCollection].fields[field].type;
|
||||
|
||||
if (['date', 'dateTime', 'time', 'timestamp'].includes(type)) {
|
||||
if (Array.isArray(compareValue)) {
|
||||
|
||||
Reference in New Issue
Block a user