From b1aaa95c9a40d8933cd89042ef58140f4aa8ffd6 Mon Sep 17 00:00:00 2001 From: ChristopherCailombus Date: Tue, 2 Dec 2025 23:19:50 -0700 Subject: [PATCH] Update referential_integrity_assertions.js 1. changed the assertion name to include the parent and child keys. This allows referential integrity checks where the child table may have multiple columns referencing the same parent column. Previously this would have given a duplicate declaration name error. 2. changed the select statement from a left join to a right join, and filtering on where parent key is null. The previous left join and filtering on child key returns only rows in parents that do not exist in child. However, this does not mean that the rows in child are also in parent. Therefore this was not a true referential integrity check. Referential integrity check should check to make sure that every child value exists in the parent values. --- includes/referential_integrity_assertions.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/referential_integrity_assertions.js b/includes/referential_integrity_assertions.js index 1c06601..6572f13 100644 --- a/includes/referential_integrity_assertions.js +++ b/includes/referential_integrity_assertions.js @@ -25,7 +25,7 @@ const assertions = []; const createReferentialIntegrityAssertion = (globalParams, parentSchema, parentTable, parentKey, parentFilter, childSchema, childTable, childKey, childFilter) => { - const assertion = assert(`assert_referential_integrity_${parentSchema}_${parentTable}_${childSchema}_${childTable}`) + const assertion = assert(`assert_referential_integrity_${parentSchema}_${parentTable}_${parentKey}_${childSchema}_${childTable}_${childKey}`) .database(globalParams.database) .schema(globalParams.schema) .description(`Check referential integrity for ${childTable}.${childKey} referencing ${parentTable}.${parentKey}`) @@ -52,8 +52,8 @@ const createReferentialIntegrityAssertion = (globalParams, parentSchema, parentT SELECT pt.${parentKey} FROM parent_filtering AS pt - LEFT JOIN child_filtering AS t ON t.${childKey} = pt.${parentKey} - WHERE t.${childKey} IS NULL + RIGHT JOIN child_filtering AS t ON t.${childKey} = pt.${parentKey} + WHERE pt.${parentKey} IS NULL `); (globalParams.tags && globalParams.tags.forEach((tag) => assertion.tags(tag)));