Skip to content

Commit

Permalink
fix: overwriting already existing query's context in childQueryOf
Browse files Browse the repository at this point in the history
This is a fix for an edge case, where a query context could be already set in a `Model.query()` call or in a custom QueryBuilder's constructor.
In our case we are defining columns with a custom `@DefaultOrderBy(columns)` decorator, they are stored to query's context after query builder is instantiated, and eventually are read in `query.onBuild()` hook to apply sorting for a query, if none other was provided to it in the meantime.
But when coupled with fetching graphs, where both parent and children models have `@DefaultOrderBy` configured, child model's query context (eager query to fetch models for a relation) was being completely overwritten with parent's query context, and that, in our case, resulted in using invalid columns to be passed to `orderBy()`, resulting in a sql error.
  • Loading branch information
falkenhawk authored and lehni committed May 19, 2023
1 parent fed87de commit 799fe91
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/queryBuilder/QueryBuilderOperationSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class QueryBuilderOperationSupport {

childQueryOf(query, { fork, isInternalQuery } = {}) {
if (query) {
let currentCtx = this.context();
let ctx = query.internalContext();

if (fork) {
Expand All @@ -165,6 +166,7 @@ class QueryBuilderOperationSupport {

this._parentQuery = query;
this.internalContext(ctx);
this.context(currentCtx);

// Use the parent's knex if there was no knex in `ctx`.
if (this.unsafeKnex() === null) {
Expand Down

0 comments on commit 799fe91

Please sign in to comment.