Fix "parenthesis"

This commit is contained in:
Adria Navarro 2024-10-15 11:34:35 +02:00
parent 76d0107d4d
commit 57da952f69
1 changed files with 14 additions and 16 deletions

View File

@ -480,9 +480,10 @@ class InternalBuilder {
subQuery = this.addJoinFieldCheck(subQuery, manyToMany) subQuery = this.addJoinFieldCheck(subQuery, manyToMany)
} }
query = query.whereExists(whereCb(subQuery)) query = query.where(q => {
q.whereExists(whereCb(subQuery))
if (allowEmptyRelationships) { if (allowEmptyRelationships) {
query = query.orWhereNotExists( q.orWhereNotExists(
joinTable.clone().innerJoin(throughTable, function () { joinTable.clone().innerJoin(throughTable, function () {
this.on( this.on(
`${fromAlias}.${manyToMany.fromPrimary}`, `${fromAlias}.${manyToMany.fromPrimary}`,
@ -492,6 +493,7 @@ class InternalBuilder {
}) })
) )
} }
})
} else { } else {
const foreignKey = `${fromAlias}.${relationship.from}` const foreignKey = `${fromAlias}.${relationship.from}`
// "join" to the main table, making sure the ID matches that of the main // "join" to the main table, making sure the ID matches that of the main
@ -1265,12 +1267,10 @@ class InternalBuilder {
}) })
: undefined : undefined
if (!throughTable) { if (!throughTable) {
// @ts-ignore
query = query.leftJoin(toTableWithSchema, function () { query = query.leftJoin(toTableWithSchema, function () {
for (let relationship of columns) { for (let relationship of columns) {
const from = relationship.from, const from = relationship.from,
to = relationship.to to = relationship.to
// @ts-ignore
this.orOn(`${fromAlias}.${from}`, "=", `${toAlias}.${to}`) this.orOn(`${fromAlias}.${from}`, "=", `${toAlias}.${to}`)
} }
}) })
@ -1281,7 +1281,6 @@ class InternalBuilder {
for (let relationship of columns) { for (let relationship of columns) {
const fromPrimary = relationship.fromPrimary const fromPrimary = relationship.fromPrimary
const from = relationship.from const from = relationship.from
// @ts-ignore
this.orOn( this.orOn(
`${fromAlias}.${fromPrimary}`, `${fromAlias}.${fromPrimary}`,
"=", "=",
@ -1293,7 +1292,6 @@ class InternalBuilder {
for (let relationship of columns) { for (let relationship of columns) {
const toPrimary = relationship.toPrimary const toPrimary = relationship.toPrimary
const to = relationship.to const to = relationship.to
// @ts-ignore
this.orOn(`${toAlias}.${toPrimary}`, `${throughAlias}.${to}`) this.orOn(`${toAlias}.${toPrimary}`, `${throughAlias}.${to}`)
} }
}) })