Skip to content

Commit a45f829

Browse files
authored
fix: rewrite to path expression, if alias for relative is already used in subselect to avoid sql error (#125)
supersedes #116
1 parent 233405c commit a45f829

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/utils.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,17 @@ const _buildSubSelect = (model, { entity, relative, element, next }, row, previo
121121
const targetAlias = _alias(element._target)
122122
const relativeAlias = _alias(relative)
123123

124-
childCqn.where(relative._relations[element.name].join(targetAlias, relativeAlias))
124+
let w = relative._relations[element.name].join(targetAlias, relativeAlias)
125+
126+
// REVISIT: rewrite to path expression, if alias for relative is already used in subselect to avoid sql error
127+
if (previousCqn?._aliases.has(relativeAlias)) {
128+
let t
129+
for (const a in entity.associations) if (entity.associations[a].target === relative.name) t = entity.associations[a]
130+
if (t && w[0]?.xpr) for (const ele of w[0].xpr) if (ele.ref?.[0] === relativeAlias) ele.ref.splice(0, 1, as, t.name)
131+
}
132+
childCqn._aliases = new Set(previousCqn ? [...previousCqn._aliases.values(), as] : [as])
133+
134+
childCqn.where(w)
125135

126136
if (previousCqn) childCqn.where('exists', previousCqn)
127137
else childCqn.where(_addKeysToWhere(keys, row, as))

test/personal-data/crud.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ const { POST: _POST, PATCH: _PATCH, GET: _GET, DELETE: _DELETE, data } = cds.tes
44

55
// the persistent outbox adds a delay
66
const wait = require('util').promisify(setTimeout)
7-
const POST = (...args) => _POST(...args).then(async res => (await wait(7), res))
8-
const PATCH = (...args) => _PATCH(...args).then(async res => (await wait(7), res))
9-
const GET = (...args) => _GET(...args).then(async res => (await wait(7), res))
10-
const DELETE = (...args) => _DELETE(...args).then(async res => (await wait(7), res))
7+
const DELAY = process.env.CI ? 42 : 7
8+
const POST = (...args) => _POST(...args).then(async res => (await wait(DELAY), res))
9+
const PATCH = (...args) => _PATCH(...args).then(async res => (await wait(DELAY), res))
10+
const GET = (...args) => _GET(...args).then(async res => (await wait(DELAY), res))
11+
const DELETE = (...args) => _DELETE(...args).then(async res => (await wait(DELAY), res))
1112

1213
// TODO: @cap-js/sqlite doesn't support structured properties
1314
// // needed for testing structured properties

0 commit comments

Comments
 (0)