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,49 +210,37 @@ 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, for (let relationship of relationships) {
function () { const from = relationship.from,
for (let relationship of relationships) { to = relationship.to
const from = relationship.from, // @ts-ignore
to = relationship.to this.orOn(`${fromTable}.${from}`, "=", `${toTable}.${to}`)
// @ts-ignore }
this.orOn(`${fromTable}.${from}`, "=", `${toTable}.${to}`) })
}
},
"left"
)
} else { } else {
query = query query = query
// @ts-ignore // @ts-ignore
.join( .leftJoin(throughTable, function () {
throughTable, for (let relationship of relationships) {
function () { const fromPrimary = relationship.fromPrimary
for (let relationship of relationships) { const from = relationship.from
const fromPrimary = relationship.fromPrimary // @ts-ignore
const from = relationship.from this.orOn(
// @ts-ignore `${fromTable}.${fromPrimary}`,
this.orOn( "=",
`${fromTable}.${fromPrimary}`, `${throughTable}.${from}`
"=", )
`${throughTable}.${from}` }
) })
} .leftJoin(toTable, function () {
}, for (let relationship of relationships) {
"left" const toPrimary = relationship.toPrimary
) const to = relationship.to
.join( // @ts-ignore
toTable, this.orOn(`${toTable}.${toPrimary}`, `${throughTable}.${to}`)
function () { }
for (let relationship of relationships) { })
const toPrimary = relationship.toPrimary
const to = relationship.to
// @ts-ignore
this.orOn(`${toTable}.${toPrimary}`, `${throughTable}.${to}`)
}
},
"left"
)
} }
} }
return query.limit(BASE_LIMIT) return query.limit(BASE_LIMIT)