From 0c2fd920b49915b65df433bbba43166e4ac6017e Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Sat, 25 May 2024 22:35:04 -0400 Subject: [PATCH] chore(db-postgres): correct joins order --- packages/db-postgres/src/find/findMany.ts | 13 ++++++------- .../db-postgres/src/queries/selectDistinct.ts | 15 +++++++-------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/packages/db-postgres/src/find/findMany.ts b/packages/db-postgres/src/find/findMany.ts index aa5ea8e1aa..6dba0a99cc 100644 --- a/packages/db-postgres/src/find/findMany.ts +++ b/packages/db-postgres/src/find/findMany.ts @@ -122,6 +122,12 @@ export const findMany = async function find({ if (pagination !== false && (orderedIDs ? orderedIDs?.length <= limit : true)) { const selectCountMethods: ChainedMethods = [] + joinAliases.forEach(({ condition, table }) => { + selectCountMethods.push({ + args: [table, condition], + method: 'leftJoin', + }) + }) Object.entries(joins).forEach(([joinTable, condition]) => { if (joinTable) { @@ -132,13 +138,6 @@ export const findMany = async function find({ } }) - joinAliases.forEach(({ condition, table }) => { - selectCountMethods.push({ - args: [table, condition], - method: 'leftJoin', - }) - }) - const countResult = await chainMethods({ methods: selectCountMethods, query: db diff --git a/packages/db-postgres/src/queries/selectDistinct.ts b/packages/db-postgres/src/queries/selectDistinct.ts index a382fe2de6..8c30980d44 100644 --- a/packages/db-postgres/src/queries/selectDistinct.ts +++ b/packages/db-postgres/src/queries/selectDistinct.ts @@ -36,6 +36,13 @@ export const selectDistinct = ({ chainedMethods.push({ args: [where], method: 'where' }) } + joinAliases.forEach(({ condition, table }) => { + chainedMethods.push({ + args: [table, condition], + method: 'leftJoin', + }) + }) + Object.entries(joins).forEach(([joinTable, condition]) => { if (joinTable) { chainedMethods.push({ @@ -44,14 +51,6 @@ export const selectDistinct = ({ }) } }) - - joinAliases.forEach(({ condition, table }) => { - chainedMethods.push({ - args: [table, condition], - method: 'leftJoin', - }) - }) - return chainMethods({ methods: chainedMethods, query: db.selectDistinct(selectFields).from(adapter.tables[tableName]),