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,18 +480,20 @@ class InternalBuilder {
subQuery = this.addJoinFieldCheck(subQuery, manyToMany) subQuery = this.addJoinFieldCheck(subQuery, manyToMany)
} }
query = query.whereExists(whereCb(subQuery)) query = query.where(q => {
if (allowEmptyRelationships) { q.whereExists(whereCb(subQuery))
query = query.orWhereNotExists( if (allowEmptyRelationships) {
joinTable.clone().innerJoin(throughTable, function () { q.orWhereNotExists(
this.on( joinTable.clone().innerJoin(throughTable, function () {
`${fromAlias}.${manyToMany.fromPrimary}`, this.on(
"=", `${fromAlias}.${manyToMany.fromPrimary}`,
`${throughAlias}.${manyToMany.from}` "=",
) `${throughAlias}.${manyToMany.from}`
}) )
) })
} )
}
})
} 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}`)
} }
}) })