Skip to content

Commit

Permalink
Fix the issue where change tracking compilation errors occur when an
Browse files Browse the repository at this point in the history
association is used as a key.
  • Loading branch information
Sv7enNowitzki committed Nov 28, 2024
1 parent 0fd23c3 commit eb74dea
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
23 changes: 19 additions & 4 deletions cds-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,32 @@ cds.on('loaded', m => {

// Determine entity keys
const keys = [], { elements: elms } = entity
for (let e in elms) if (elms[e].key) keys.push(e)
for (let e in elms) {
if (elms[e].key) {
if (elms[e].type === 'cds.Association') {
keys.push({
e,
val: elms[e]
});
} else {
keys.push(e);
}
}
}

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

// Add association to ChangeView...
const on = [...changes.on]; keys.forEach((k, i) => { i && on.push('||'); on.push({
ref: k === 'up_' ? [k,'ID'] : [k] // REVISIT: up_ handling is a dirty hack for now
})})
const on = [...changes.on];
keys.forEach((k, i) => {
i && on.push("||");
on.push({
ref: k?.val?.type === "cds.Association" ? [k.e, k.val.keys?.[0]?.ref?.[0]] : [k]
});
});
const assoc = { ...changes, on }
const query = entity.projection || entity.query?.SELECT
if(!entity['@changelog.disable_assoc'])
Expand Down
18 changes: 18 additions & 0 deletions tests/bookshop/db/schema.cds
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,21 @@ entity City : cuid {
entity Country : cuid {
countryName : CountryName;
}

entity FirstEntity : managed, cuid {
name : String;
children : Association to one Children;
}

entity SecondEntity : managed, cuid {
name : String;
children : Association to one Children;
}

@changelog : [one_ID]
entity Children : managed {
@changelog
key one : Association to one FirstEntity;
@changelog
key two : Association to one SecondEntity;
}

0 comments on commit eb74dea

Please sign in to comment.