From 914e5af35c01d96754b9aa267c1ed4ff4062d43c Mon Sep 17 00:00:00 2001 From: James Ahlborn Date: Mon, 21 Mar 2016 02:15:47 +0000 Subject: Allow null values in foreign key fields when enforcing referential integrity. Fixes issue #136 git-svn-id: https://svn.code.sf.net/p/jackcess/code/jackcess/trunk@980 f203690c-595d-4dc9-a70b-905162fa7fd2 --- .../com/healthmarketscience/jackcess/impl/FKEnforcer.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/main') diff --git a/src/main/java/com/healthmarketscience/jackcess/impl/FKEnforcer.java b/src/main/java/com/healthmarketscience/jackcess/impl/FKEnforcer.java index bf5138c..470035d 100644 --- a/src/main/java/com/healthmarketscience/jackcess/impl/FKEnforcer.java +++ b/src/main/java/com/healthmarketscience/jackcess/impl/FKEnforcer.java @@ -219,8 +219,8 @@ final class FKEnforcer throws IOException { // ensure that the relevant rows exist in the primary tables for which - // this table is a secondary table. - if(!joiner.hasRows(row)) { + // this table is a secondary table. however, null values are allowed + if(!areNull(joiner, row) && !joiner.hasRows(row)) { throw new ConstraintViolationException( "Adding new row " + Arrays.asList(row) + " violates constraint " + joiner.toFKString()); @@ -288,6 +288,15 @@ final class FKEnforcer return false; } + private static boolean areNull(Joiner joiner, Object[] row) { + for(Index.Column col : joiner.getColumns()) { + if(col.getColumn().getRowValue(row) != null) { + return false; + } + } + return true; + } + private boolean enforcing() { return _table.getDatabase().isEnforceForeignKeys(); } -- cgit v1.2.3