Skip to content

Commit

Permalink
fix: Do not generate UUIDs for association keys (#398)
Browse files Browse the repository at this point in the history
Fixes : https://github.tools.sap/cap/cdsnode/issues/1699

---------

Co-authored-by: Bob den Os <108393871+BobdenOs@users.noreply.github.com>
  • Loading branch information
mariayord and BobdenOs authored Dec 28, 2023
1 parent 23d880f commit 9970e14
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion db-service/lib/fill-in-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ const generateUUIDandPropagateKeys = (target, data, event) => {
if (!data) return
const keys = target.keys
for (const key in keys) {
if (keys[key].type === 'cds.UUID' && !data[key] && event === 'CREATE') {
const keyElement = keys[key]
if (
keyElement.type === 'cds.UUID' &&
!data[key] && event === 'CREATE' &&
!keyElement.parent.elements[keyElement._foreignKey4]?._isAssociationStrict
) {
data[key] = cds.utils.uuid()
}
}
Expand Down
10 changes: 10 additions & 0 deletions sqlite/test/general/model.cds
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ service test {
key ID : UUID;
}

entity BooksWithAssocAsKey {
key author: Association to AuthorAssoc;
title : String;
stock : Integer;
}

entity AuthorAssoc {
key ID: UUID;
}

entity fooLocalized {
key ID : Integer;
text : localized String;
Expand Down
7 changes: 7 additions & 0 deletions sqlite/test/general/uuid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ describe('UUID Generation', () => {
await DELETE('test.bar')
})
})

test('INSERT entity with missing key as association throws error', async () => {
expect.assertions(1)
return expect(
cds.run(INSERT.into('test.BooksWithAssocAsKey').entries([{}]))
).rejects.toMatchObject({ code: 'SQLITE_CONSTRAINT_NOTNULL' })
})
})

0 comments on commit 9970e14

Please sign in to comment.