Skip to content

Commit

Permalink
Fix join using builder withSchema. (#2744)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Lidén authored and elhigu committed Aug 4, 2018
1 parent 30bab69 commit 2183a27
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/query/builder.js
Expand Up @@ -176,7 +176,11 @@ assign(Builder.prototype, {
} else if (joinType === 'raw') {
join = new JoinClause(this.client.raw(table, first), 'raw');
} else {
join = new JoinClause(table, joinType, schema);
join = new JoinClause(
table,
joinType,
table instanceof Builder ? undefined : schema
);
if (arguments.length > 1) {
join.on.apply(join, toArray(arguments).slice(1));
}
Expand Down
31 changes: 31 additions & 0 deletions test/unit/query/builder.js
Expand Up @@ -8236,4 +8236,35 @@ describe('QueryBuilder', function() {
oracledb: 'select 0',
});
});

it('join with subquery using .withSchema', function() {
testsql(
qb()
.from('departments')
.withSchema('foo')
.join(
qb()
.from('trainees')
.withSchema('foo')
.groupBy('department_id')
.select('department_id', raw('count(*)'))
.as('trainee_cnts'),
'trainee_cnts.department_id',
'departments.id'
)
.select('departments.*', 'trainee_cnts.count as trainee_cnt'),
{
pg:
'select "departments".*, "trainee_cnts"."count" as "trainee_cnt" from "foo"."departments" inner join (select "department_id", count(*) from "foo"."trainees" group by "department_id") as "trainee_cnts" on "trainee_cnts"."department_id" = "departments"."id"',
mysql:
'select `departments`.*, `trainee_cnts`.`count` as `trainee_cnt` from `foo`.`departments` inner join (select `department_id`, count(*) from `foo`.`trainees` group by `department_id`) as `trainee_cnts` on `trainee_cnts`.`department_id` = `departments`.`id`',
mssql:
'select [departments].*, [trainee_cnts].[count] as [trainee_cnt] from [foo].[departments] inner join (select [department_id], count(*) from [foo].[trainees] group by [department_id]) as [trainee_cnts] on [trainee_cnts].[department_id] = [departments].[id]',
'pg-redshift':
'select "departments".*, "trainee_cnts"."count" as "trainee_cnt" from "foo"."departments" inner join (select "department_id", count(*) from "foo"."trainees" group by "department_id") as "trainee_cnts" on "trainee_cnts"."department_id" = "departments"."id"',
oracledb:
'select "departments".*, "trainee_cnts"."count" "trainee_cnt" from "foo"."departments" inner join (select "department_id", count(*) from "foo"."trainees" group by "department_id") "trainee_cnts" on "trainee_cnts"."department_id" = "departments"."id"',
}
);
});
});

0 comments on commit 2183a27

Please sign in to comment.