Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Do not generate UUIDs for association keys #398

Merged
merged 7 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 10 additions & 0 deletions sqlite/test/general/uuid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,14 @@ describe('UUID Generation', () => {
await DELETE('test.bar')
})
})

test('INSERT entity with missing key as association throws error', async () => {
const db = await cds.connect.to('db')
return db.tx(async () => {
expect.assertions(1)
await expect(INSERT.into('test.BooksWithAssocAsKey').entries([{}])).rejects.toThrow(
'NOT NULL constraint failed: test_BooksWithAssocAsKey.author_ID'
)
})
})
})
Loading