Skip to content

Commit a000383

Browse files
Fix the issue where change tracking compilation errors occur when an association is used as a key. (#139)
Although we currently do not support the use of associations as keys, it should not cause compilation errors in this case, leading to the project being halted. Therefore, this fix is needed to ensure that the project does not stop.
1 parent 1d8f66c commit a000383

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

cds-plugin.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,32 @@ cds.on('loaded', m => {
156156

157157
// Determine entity keys
158158
const keys = [], { elements: elms } = entity
159-
for (let e in elms) if (elms[e].key) keys.push(e)
159+
for (let e in elms) {
160+
if (elms[e].key) {
161+
if (elms[e].type === 'cds.Association') {
162+
keys.push({
163+
e,
164+
val: elms[e]
165+
});
166+
} else {
167+
keys.push(e);
168+
}
169+
}
170+
}
160171

161172
// If no key attribute is defined for the entity, the logic to add association to ChangeView should be skipped.
162173
if(keys.length === 0) {
163174
continue;
164175
}
165176

166177
// Add association to ChangeView...
167-
const on = [...changes.on]; keys.forEach((k, i) => { i && on.push('||'); on.push({
168-
ref: k === 'up_' ? [k,'ID'] : [k] // REVISIT: up_ handling is a dirty hack for now
169-
})})
178+
const on = [...changes.on];
179+
keys.forEach((k, i) => {
180+
i && on.push("||");
181+
on.push({
182+
ref: k?.val?.type === "cds.Association" ? [k.e, k.val.keys?.[0]?.ref?.[0]] : [k]
183+
});
184+
});
170185
const assoc = { ...changes, on }
171186
const query = entity.projection || entity.query?.SELECT
172187
if(!entity['@changelog.disable_assoc'])

tests/bookshop/db/schema.cds

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,21 @@ entity City : cuid {
261261
entity Country : cuid {
262262
countryName : CountryName;
263263
}
264+
265+
entity FirstEntity : managed, cuid {
266+
name : String;
267+
children : Association to one Children;
268+
}
269+
270+
entity SecondEntity : managed, cuid {
271+
name : String;
272+
children : Association to one Children;
273+
}
274+
275+
@changelog : [one_ID]
276+
entity Children : managed {
277+
@changelog
278+
key one : Association to one FirstEntity;
279+
@changelog
280+
key two : Association to one SecondEntity;
281+
}

0 commit comments

Comments
 (0)