From 28c0497ada17cb0b0420c606400464b919679e38 Mon Sep 17 00:00:00 2001 From: Mariya Yordanova Date: Wed, 27 Dec 2023 22:02:35 +0100 Subject: [PATCH 1/7] fix: Do not generate UUIDs for association keys --- db-service/lib/fill-in-keys.js | 3 ++- sqlite/test/general/model.cds | 10 ++++++++++ sqlite/test/general/uuid.test.js | 14 +++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/db-service/lib/fill-in-keys.js b/db-service/lib/fill-in-keys.js index e73755246..ec1bee4d3 100644 --- a/db-service/lib/fill-in-keys.js +++ b/db-service/lib/fill-in-keys.js @@ -8,7 +8,8 @@ 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 keyValue = keys[key] + if (keyValue.type === 'cds.UUID' && !data[key] && event === 'CREATE' && !keyValue.parent.elements[keyValue._foreignKey4]?._isAssociationStrict) { data[key] = cds.utils.uuid() } } diff --git a/sqlite/test/general/model.cds b/sqlite/test/general/model.cds index 61d263f7a..0a72652f7 100644 --- a/sqlite/test/general/model.cds +++ b/sqlite/test/general/model.cds @@ -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; diff --git a/sqlite/test/general/uuid.test.js b/sqlite/test/general/uuid.test.js index 6cb0deb27..ac756bec4 100644 --- a/sqlite/test/general/uuid.test.js +++ b/sqlite/test/general/uuid.test.js @@ -25,4 +25,16 @@ 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 () => { + try { + await INSERT.into('test.BooksWithAssocAsKey').entries([{}]) + expect(1).to.be.eq('INSERT entity with missing key as association throws error') + } catch (err) { + expect(err.code).toEqual('SQLITE_CONSTRAINT_NOTNULL') + } + }) + }) +}) \ No newline at end of file From e4b050d014329176a85f3b6dbc6e2c75cf34cdcc Mon Sep 17 00:00:00 2001 From: Mariya Yordanova Date: Wed, 27 Dec 2023 22:08:02 +0100 Subject: [PATCH 2/7] formatting --- sqlite/test/general/uuid.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlite/test/general/uuid.test.js b/sqlite/test/general/uuid.test.js index ac756bec4..e55182405 100644 --- a/sqlite/test/general/uuid.test.js +++ b/sqlite/test/general/uuid.test.js @@ -37,4 +37,4 @@ describe('UUID Generation', () => { } }) }) -}) \ No newline at end of file +}) From 77fbbf4ac6d46cc26d52c59f78562121506babb7 Mon Sep 17 00:00:00 2001 From: Mariya Yordanova Date: Thu, 28 Dec 2023 10:51:15 +0100 Subject: [PATCH 3/7] reworked comments --- db-service/lib/fill-in-keys.js | 8 ++++++-- sqlite/test/general/uuid.test.js | 10 ++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/db-service/lib/fill-in-keys.js b/db-service/lib/fill-in-keys.js index ec1bee4d3..261849590 100644 --- a/db-service/lib/fill-in-keys.js +++ b/db-service/lib/fill-in-keys.js @@ -8,8 +8,12 @@ const generateUUIDandPropagateKeys = (target, data, event) => { if (!data) return const keys = target.keys for (const key in keys) { - const keyValue = keys[key] - if (keyValue.type === 'cds.UUID' && !data[key] && event === 'CREATE' && !keyValue.parent.elements[keyValue._foreignKey4]?._isAssociationStrict) { + const keyElement = keys[key] + if ( + keyElement.type === 'cds.UUID' && + !data[key] && event === 'CREATE' && + !keyElement.parent.elements[keyElement._foreignKey4]?._isAssociationStrict + ) { data[key] = cds.utils.uuid() } } diff --git a/sqlite/test/general/uuid.test.js b/sqlite/test/general/uuid.test.js index e55182405..601cce9b4 100644 --- a/sqlite/test/general/uuid.test.js +++ b/sqlite/test/general/uuid.test.js @@ -29,12 +29,10 @@ describe('UUID Generation', () => { test('INSERT entity with missing key as association throws error', async () => { const db = await cds.connect.to('db') return db.tx(async () => { - try { - await INSERT.into('test.BooksWithAssocAsKey').entries([{}]) - expect(1).to.be.eq('INSERT entity with missing key as association throws error') - } catch (err) { - expect(err.code).toEqual('SQLITE_CONSTRAINT_NOTNULL') - } + expect.assertions(1) + await expect(INSERT.into('test.BooksWithAssocAsKey').entries([{}])).rejects.toThrow( + 'NOT NULL constraint failed: test_BooksWithAssocAsKey.author_ID' + ) }) }) }) From ce581af95e9f5c1fe2a3643b6737e1665edf7ed4 Mon Sep 17 00:00:00 2001 From: Mariya Yordanova Date: Thu, 28 Dec 2023 11:41:39 +0100 Subject: [PATCH 4/7] fix test --- sqlite/test/general/uuid.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sqlite/test/general/uuid.test.js b/sqlite/test/general/uuid.test.js index 601cce9b4..677e594b2 100644 --- a/sqlite/test/general/uuid.test.js +++ b/sqlite/test/general/uuid.test.js @@ -28,11 +28,11 @@ describe('UUID Generation', () => { 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' - ) - }) + expect.assertions(1) + return expect(db.tx(async () => { + await INSERT.into('test.BooksWithAssocAsKey').entries([{}]) + })).rejects.toThrow( + 'NOT NULL constraint failed: test_BooksWithAssocAsKey.author_ID' + ) }) }) From 702a5d771b7e9b16906644dad58786abd9ebfdf7 Mon Sep 17 00:00:00 2001 From: Mariya Yordanova Date: Thu, 28 Dec 2023 13:33:01 +0100 Subject: [PATCH 5/7] test with toEqual --- sqlite/test/general/uuid.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sqlite/test/general/uuid.test.js b/sqlite/test/general/uuid.test.js index 677e594b2..c990e8f2c 100644 --- a/sqlite/test/general/uuid.test.js +++ b/sqlite/test/general/uuid.test.js @@ -31,8 +31,8 @@ describe('UUID Generation', () => { expect.assertions(1) return expect(db.tx(async () => { await INSERT.into('test.BooksWithAssocAsKey').entries([{}]) - })).rejects.toThrow( - 'NOT NULL constraint failed: test_BooksWithAssocAsKey.author_ID' + })).rejects.toEqual( + new Error('NOT NULL constraint failed: test_BooksWithAssocAsKey.author_ID') ) }) }) From 8a0c7235f412e4fc326dc578224dc6cd91314893 Mon Sep 17 00:00:00 2001 From: Mariya Yordanova Date: Thu, 28 Dec 2023 13:58:51 +0100 Subject: [PATCH 6/7] fix the reject --- sqlite/test/general/uuid.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sqlite/test/general/uuid.test.js b/sqlite/test/general/uuid.test.js index c990e8f2c..c6fe9f2cc 100644 --- a/sqlite/test/general/uuid.test.js +++ b/sqlite/test/general/uuid.test.js @@ -31,8 +31,8 @@ describe('UUID Generation', () => { expect.assertions(1) return expect(db.tx(async () => { await INSERT.into('test.BooksWithAssocAsKey').entries([{}]) - })).rejects.toEqual( - new Error('NOT NULL constraint failed: test_BooksWithAssocAsKey.author_ID') + })).rejects.toMatchObject( + {code: 'SQLITE_CONSTRAINT_NOTNULL'} ) }) }) From fb77de917b18e0ab21463b5e1376f1b20105a3e7 Mon Sep 17 00:00:00 2001 From: mariayord Date: Thu, 28 Dec 2023 14:11:59 +0100 Subject: [PATCH 7/7] Update sqlite/test/general/uuid.test.js Co-authored-by: Bob den Os <108393871+BobdenOs@users.noreply.github.com> --- sqlite/test/general/uuid.test.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/sqlite/test/general/uuid.test.js b/sqlite/test/general/uuid.test.js index c6fe9f2cc..2c449cbba 100644 --- a/sqlite/test/general/uuid.test.js +++ b/sqlite/test/general/uuid.test.js @@ -27,12 +27,9 @@ describe('UUID Generation', () => { }) test('INSERT entity with missing key as association throws error', async () => { - const db = await cds.connect.to('db') expect.assertions(1) - return expect(db.tx(async () => { - await INSERT.into('test.BooksWithAssocAsKey').entries([{}]) - })).rejects.toMatchObject( - {code: 'SQLITE_CONSTRAINT_NOTNULL'} - ) + return expect( + cds.run(INSERT.into('test.BooksWithAssocAsKey').entries([{}])) + ).rejects.toMatchObject({ code: 'SQLITE_CONSTRAINT_NOTNULL' }) }) })