diff --git a/lib/make/makeJoinForPipeline.js b/lib/make/makeJoinForPipeline.js index 612a0dbf..2d04a66d 100644 --- a/lib/make/makeJoinForPipeline.js +++ b/lib/make/makeJoinForPipeline.js @@ -322,7 +322,9 @@ function tableJoin( ? `${localPart.schema ? localPart.schema + '.' : ''}${ localPart.table }.${localPart.column}` - : `${previousJoin.as || previousJoin.table}.${localPart.column}`; + : previousJoin.as + ? `${previousJoin.as}.${localPart.column}` + : localPart.column; const foreignField = fromPart.column; pipeline.push({ $lookup: { diff --git a/package.json b/package.json index d0aff77c..f630473c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@synatic/noql", - "version": "4.1.14", + "version": "4.1.15", "description": "Convert SQL statements to mongo queries or aggregates", "main": "index.js", "files": [ diff --git a/test/aggregateTests/joins.json b/test/aggregateTests/joins.json index 16141f20..8dd397f9 100644 --- a/test/aggregateTests/joins.json +++ b/test/aggregateTests/joins.json @@ -49,7 +49,7 @@ "$lookup": { "from": "inventory", "as": "inventory", - "localField": "orders.item", + "localField": "item", "foreignField": "sku" } }, diff --git a/test/joins/join-cases.json b/test/joins/join-cases.json index 9e21d276..1b9fb970 100644 --- a/test/joins/join-cases.json +++ b/test/joins/join-cases.json @@ -916,6 +916,24 @@ "i": [] } ] + }, + "no-alias": { + "case-1": { + "expectedResults": [ + { + "item": "almonds", + "sku": [ + "almonds" + ] + }, + { + "item": "pecans", + "sku": [ + "pecans" + ] + } + ] + } } }, "n-level-join": { diff --git a/test/joins/joins.test.js b/test/joins/joins.test.js index c1f94cb2..d8631206 100644 --- a/test/joins/joins.test.js +++ b/test/joins/joins.test.js @@ -194,6 +194,27 @@ describe('joins', function () { casePath: 'left-join.basic-reversed', }); }); + + it('should not need table aliases', async () => { + const queryString = ` + select item, + inventory.sku + from + \`orders\` + left outer JOIN \`inventory\` on \`inventory\`.\`sku\` = \`item\` + LIMIT 2 + `; + const {pipeline, results} = await queryResultTester({ + queryString, + casePath: 'left-join.no-alias.case-1', + mode: 'write', + skipDbQuery: false, + }); + const lookup = pipeline.find((p) => !!p.$lookup); + for (const result of results) { + assert.deepEqual(result.item, result.sku[0]); + } + }); }); describe('join order of queries', () => {