Fix for #4431 - converting SQL joins back to left joins, syntax specifying join type as string is old which doesn't work in latest versions of knex.

This commit is contained in:
mike12345567 2022-02-11 14:04:23 +00:00
parent 7f7bd8f447
commit 7109b34ea3
2 changed files with 29 additions and 41 deletions

View File

@ -541,7 +541,7 @@ module External {
if (!linkTable || !linkPrimary) { if (!linkTable || !linkPrimary) {
return return
} }
const rows = related[key].rows || [] const rows = related[key]?.rows || []
const found = rows.find( const found = rows.find(
(row: { [key: string]: any }) => (row: { [key: string]: any }) =>
row[linkPrimary] === relationship.id || row[linkPrimary] === relationship.id ||

View File

@ -210,24 +210,18 @@ class InternalBuilder {
const { toTable, throughTable } = JSON.parse(key) const { toTable, throughTable } = JSON.parse(key)
if (!throughTable) { if (!throughTable) {
// @ts-ignore // @ts-ignore
query = query.join( query = query.leftJoin(toTable, function () {
toTable,
function () {
for (let relationship of relationships) { for (let relationship of relationships) {
const from = relationship.from, const from = relationship.from,
to = relationship.to to = relationship.to
// @ts-ignore // @ts-ignore
this.orOn(`${fromTable}.${from}`, "=", `${toTable}.${to}`) this.orOn(`${fromTable}.${from}`, "=", `${toTable}.${to}`)
} }
}, })
"left"
)
} else { } else {
query = query query = query
// @ts-ignore // @ts-ignore
.join( .leftJoin(throughTable, function () {
throughTable,
function () {
for (let relationship of relationships) { for (let relationship of relationships) {
const fromPrimary = relationship.fromPrimary const fromPrimary = relationship.fromPrimary
const from = relationship.from const from = relationship.from
@ -238,21 +232,15 @@ class InternalBuilder {
`${throughTable}.${from}` `${throughTable}.${from}`
) )
} }
}, })
"left" .leftJoin(toTable, function () {
)
.join(
toTable,
function () {
for (let relationship of relationships) { for (let relationship of relationships) {
const toPrimary = relationship.toPrimary const toPrimary = relationship.toPrimary
const to = relationship.to const to = relationship.to
// @ts-ignore // @ts-ignore
this.orOn(`${toTable}.${toPrimary}`, `${throughTable}.${to}`) this.orOn(`${toTable}.${toPrimary}`, `${throughTable}.${to}`)
} }
}, })
"left"
)
} }
} }
return query.limit(BASE_LIMIT) return query.limit(BASE_LIMIT)