From a175c52de9afb0c2ff9cde6d3386ea7148032ff2 Mon Sep 17 00:00:00 2001 From: Patrice Bender Date: Wed, 12 Feb 2025 20:39:27 +0100 Subject: [PATCH] no need to add `is null` check for unmanaged --- db-service/lib/assert-constraint.js | 11 ++++++----- test/bookshop/db/schema.cds | 3 ++- test/scenarios/bookshop/assert-constraint.test.js | 12 +++++++++++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/db-service/lib/assert-constraint.js b/db-service/lib/assert-constraint.js index 1cadb59a0..ca8670d1e 100644 --- a/db-service/lib/assert-constraint.js +++ b/db-service/lib/assert-constraint.js @@ -61,11 +61,8 @@ function attachConstraints(_results, req) { const { condition, element } = constraint const xpr = [] // if the element is nullable, we prepend xpr with ` IS NULL OR …` - if (!element.notNull) { - if (element.on) - // null check the whole xpr for unmanaged assocs which vanish in the result - xpr.unshift({ xpr: condition.xpr }, 'is', 'null', 'or') - else xpr.unshift({ ref: [element.name] }, 'is', 'null', 'or') + if (!element.notNull && !element.on) { + xpr.unshift({ ref: [element.name] }, 'is', 'null', 'or') } xpr.push({ xpr: condition.xpr }) return { @@ -138,6 +135,10 @@ function attachConstraints(_results, req) { return elmConstraints } } + + function wrapInCaseWhen(xpr) { + return ['case', 'when', { xpr }, 'then', { val: true }, 'else', { val: false }, 'end'] + } } async function checkConstraints(req) { diff --git a/test/bookshop/db/schema.cds b/test/bookshop/db/schema.cds index ecfe90184..2f7922e69 100644 --- a/test/bookshop/db/schema.cds +++ b/test/bookshop/db/schema.cds @@ -60,7 +60,8 @@ entity Genres : sap.common.CodeList { @assert.constraint: ( children.name in ( 'Fiction', 'Drama', 'Poetry', 'Fantasy', 'Science Fiction', 'Romance', 'Mystery', 'Thriller', 'Dystopia', 'Fairy Tale', - 'Non-Fiction', 'Biography', 'Autobiography', 'Essay', 'Speech' + 'Non-Fiction', 'Biography', 'Autobiography', 'Essay', 'Speech', + 'New Genre', 'New Sub-Genre' ) ) children : Composition of many Genres on children.parent = $self; diff --git a/test/scenarios/bookshop/assert-constraint.test.js b/test/scenarios/bookshop/assert-constraint.test.js index 7b77b2c5a..7948dc873 100644 --- a/test/scenarios/bookshop/assert-constraint.test.js +++ b/test/scenarios/bookshop/assert-constraint.test.js @@ -102,9 +102,19 @@ describe('Bookshop - assertions', () => { ] }, { auth: { username: 'alice' } }) ).to.be.rejectedWith(/@assert.constraint ”children” failed/) - // nothing should have been created const genre = await SELECT.from(Genres).where('ID in (90, 91)') expect(genre).to.be.empty + // positive case + await POST('admin/Genres', { + ID: 100, + name: 'New Genre', // OK + children: [ + { ID: 101, name: 'New Sub-Genre' }, // also OK + ], + }, { auth: { username: 'alice' } }) + const genres = await SELECT.from(Genres).where('ID in (100, 101)') + // both should have been created + expect(genres).to.have.length(2) }) }) })