Merge pull request #4432 from Budibase/fix/4431

Fix for joins in SQL tables
This commit is contained in:
Michael Drury 2022-02-11 14:41:35 +00:00 committed by GitHub
commit 0bd75503ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)