diff --git a/.changeset/many-feet-rule.md b/.changeset/many-feet-rule.md new file mode 100644 index 00000000000..cc016f5448a --- /dev/null +++ b/.changeset/many-feet-rule.md @@ -0,0 +1,5 @@ +--- +'@keystonejs/adapter-knex': patch +--- + +Bug fix: "order by" crash on non real keys when list first field is a relationship diff --git a/packages/adapter-knex/lib/adapter-knex.js b/packages/adapter-knex/lib/adapter-knex.js index 9179e090adc..a0d522ccbe4 100644 --- a/packages/adapter-knex/lib/adapter-knex.js +++ b/packages/adapter-knex/lib/adapter-knex.js @@ -648,17 +648,25 @@ class QueryBuilder { // SELECT ... ORDER BY const [orderField, orderDirection] = this._getOrderFieldAndDirection(orderBy); const sortKey = listAdapter.fieldAdaptersByPath[orderField].sortKey || orderField; - this._query.orderBy(sortKey, orderDirection); + if (listAdapter.realKeys.includes(sortKey)) { + this._query.orderBy(sortKey, orderDirection); + } } if (sortBy !== undefined) { // SELECT ... ORDER BY [, , ...] this._query.orderBy( - sortBy.map(s => { - const [orderField, orderDirection] = this._getOrderFieldAndDirection(s); - const sortKey = listAdapter.fieldAdaptersByPath[orderField].sortKey || orderField; + sortBy + .map(s => { + const [orderField, orderDirection] = this._getOrderFieldAndDirection(s); + const sortKey = listAdapter.fieldAdaptersByPath[orderField].sortKey || orderField; - return { column: sortKey, order: orderDirection }; - }) + if (listAdapter.realKeys.includes(sortKey)) { + return { column: sortKey, order: orderDirection }; + } else { + return undefined; + } + }) + .filter(s => typeof s !== 'undefined') ); } }