Skip to content

Commit bdad412

Browse files
authored
fix: tree table with expand (#1363)
The foreign key in the case of an expand was not included in the inner SELECT which resulted in the error "no such column".
1 parent c290572 commit bdad412

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

db-service/lib/cqn2sql.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,13 @@ class CQN2SQLRenderer {
350350
if (element['@Core.Computed'] && name in availableComputedColumns) continue
351351
if (name.toUpperCase() in reservedColumnNames) ref.as = `$$${name}$$`
352352
columnsIn.push(ref)
353-
if (from.args || columnsFiltered.find(c => this.column_name(c) === name)) {
353+
const foreignkey4 = element._foreignKey4
354+
if (
355+
from.args ||
356+
columnsFiltered.find(c => this.column_name(c) === name) ||
357+
// foreignkey needs to be included when the association is expanded
358+
(foreignkey4 && q.SELECT.columns.some(c => c.element?.isAssociation && c.element.name === foreignkey4))
359+
) {
354360
columnsOut.push(ref.as ? { ref: [ref.as], as: name } : ref)
355361
}
356362
}

test/scenarios/bookshop/hierarchy.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ describe('Bookshop - Genres', () => {
3232
await GET(`/tree/Genres?$select=DrillState,ID,name&$apply=${topLevels}(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID')`)
3333
})
3434

35+
test('TopLevels with $expand', async () => {
36+
const query = `/tree/Genres?$select=DrillState,ID,name&$apply=${topLevels}(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=2)&$select=DrillState,ID,name&$expand=parent($select=ID,name)`
37+
const res = await GET(query)
38+
39+
// should have parent expanded
40+
const hasParent = res.data.value.some(item => item.parent)
41+
expect(hasParent).to.be.true
42+
})
43+
3544
test('ancestors($filter)/TopLevels(1)', async () => {
3645
const res = await GET(`/tree/Genres?$select=DrillState,ID,name&$apply=ancestors($root/GenreHierarchy,GenreHierarchy,ID,filter(tolower(name) eq tolower('Fantasy')),keep start)/${topLevels}(HierarchyNodes=$root/GenreHierarchy,HierarchyQualifier='GenreHierarchy',NodeProperty='ID',Levels=1)`)
3746
expect(res).property('data').property('value').deep.eq([

0 commit comments

Comments
 (0)