You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2026/10/19 23:06:45 UTC

(cayenne) 02/02: DbAttribute.isForeignKey() is conceptually wrong, removing

This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git

commit 69e71b8bea7f291bd1892822ee54a721a49819b1
Author: Andrus Adamchik <an...@objectstyle.com>
AuthorDate: Mon Oct 19 18:56:01 2026 -0400

    DbAttribute.isForeignKey() is conceptually wrong, removing
---
 .../dbsync/merge/context/EntityMergeSupport.java   | 26 ++++++++++--
 .../org/apache/cayenne/dbsync/merge/MergeCase.java | 39 +++++++++++++-----
 .../autorelationship/InferRelationshipsDialog.java | 18 +++++++-
 .../java/org/apache/cayenne/map/DbAttribute.java   | 27 ------------
 .../org/apache/cayenne/unit/DbSchemaManager.java   | 48 +++++++++++++---------
 5 files changed, 97 insertions(+), 61 deletions(-)

diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/context/EntityMergeSupport.java b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/context/EntityMergeSupport.java
index 305e8e4cf..00e08bb04 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/context/EntityMergeSupport.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/context/EntityMergeSupport.java
@@ -43,10 +43,12 @@ import java.sql.Types;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Set;
 
 /**
  * Implements methods for entity merging.
@@ -323,13 +325,29 @@ public class EntityMergeSupport {
      * @since 1.2
      */
     public Collection<DbAttribute> getMeaningfulFKs(ObjEntity objEntity) {
-        List<DbAttribute> fks = new ArrayList<>(2);
+        DbEntity dbEntity = objEntity.getDbEntity();
+        if (dbEntity == null) {
+            return List.of();
+        }
+
+        // columns that a to-one relationship joins to a PK, taken one join at a time even when the relationship's
+        // other joins don't hit a PK. Same per-join rule as AshwoodEntitySorter
+        Set<DbAttribute> joinedToPK = new HashSet<>();
+        for (DbRelationship relationship : dbEntity.getRelationships()) {
+            if (relationship.isToMany() || relationship.isToDependentPK()) {
+                continue;
+            }
+            for (DbJoin join : relationship.getJoins()) {
+                if (join.getTarget() != null && join.getTarget().isPrimaryKey()) {
+                    joinedToPK.add(join.getSource());
+                }
+            }
+        }
 
+        List<DbAttribute> fks = new ArrayList<>(2);
         for (ObjAttribute property : objEntity.getAttributes()) {
             DbAttribute column = property.getDbAttribute();
-
-            // check if adding it makes sense at all
-            if (column != null && column.isForeignKey()) {
+            if (column != null && joinedToPK.contains(column)) {
                 fks.add(column);
             }
         }
diff --git a/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/merge/MergeCase.java b/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/merge/MergeCase.java
index b86ed2755..f246b59be 100644
--- a/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/merge/MergeCase.java
+++ b/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/merge/MergeCase.java
@@ -39,6 +39,8 @@ import org.apache.cayenne.dbsync.reverse.filters.TableFilter;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbJoin;
+import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.EntityResolver;
 import org.apache.cayenne.CayenneRuntime;
 import org.apache.cayenne.unit.CayenneProjects;
@@ -55,6 +57,7 @@ import java.sql.Statement;
 import java.sql.Types;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
@@ -179,6 +182,30 @@ public abstract class MergeCase {
         return tokensOut;
     }
 
+    private static final Set<Integer> BINARY_TYPES = Set.of(Types.BINARY, Types.VARBINARY, Types.LONGVARBINARY);
+
+    // a table can't exist on a DB without binary PK support if its own PK is binary, or if one of its to-one
+    // relationships joins to a binary PK (the join column has the PK type). Same per-join rule as AshwoodEntitySorter
+    private static boolean hasBinaryKey(DbEntity entity) {
+        for (DbAttribute pk : entity.getPrimaryKeys()) {
+            if (BINARY_TYPES.contains(pk.getType())) {
+                return true;
+            }
+        }
+        for (DbRelationship relationship : entity.getRelationships()) {
+            if (relationship.isToMany() || relationship.isToDependentPK()) {
+                continue;
+            }
+            for (DbJoin join : relationship.getJoins()) {
+                DbAttribute target = join.getTarget();
+                if (target != null && target.isPrimaryKey() && BINARY_TYPES.contains(target.getType())) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
     /**
      * Remote binary pk {@link DbEntity} for {@link DbAdapter} not supporting
      * that and so on.
@@ -194,16 +221,8 @@ public abstract class MergeCase {
         List<DbEntity> entitiesToRemove = new ArrayList<DbEntity>();
 
         for (DbEntity ent : map.getDbEntities()) {
-            for (DbAttribute attr : ent.getAttributes()) {
-                // check for BIN PK or FK to BIN Pk
-                if (attr.getType() == Types.BINARY || attr.getType() == Types.VARBINARY
-                        || attr.getType() == Types.LONGVARBINARY) {
-
-                    if (attr.isPrimaryKey() || attr.isForeignKey()) {
-                        entitiesToRemove.add(ent);
-                        break;
-                    }
-                }
+            if (hasBinaryKey(ent)) {
+                entitiesToRemove.add(ent);
             }
         }
 
diff --git a/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/autorelationship/InferRelationshipsDialog.java b/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/autorelationship/InferRelationshipsDialog.java
index 5ba252289..5f63d2b7a 100644
--- a/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/autorelationship/InferRelationshipsDialog.java
+++ b/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/ui/autorelationship/InferRelationshipsDialog.java
@@ -220,7 +220,7 @@ public class InferRelationshipsDialog extends ProjectDialog {
                         && !attribute.isPrimaryKey()
                         && !targetEntity.getAttributes().isEmpty()) {
 
-                    if (!attribute.isForeignKey()) {
+                    if (!isJoinedToPK(entity, attribute)) {
                         InferredRelationship myir = new InferredRelationship();
                         myir.setSource(entity);
                         myir.setTarget(targetEntity);
@@ -232,6 +232,22 @@ public class InferRelationshipsDialog extends ProjectDialog {
         }
     }
 
+    // true if a to-one relationship of the entity already joins this attribute to a PK, so there is nothing to infer
+    // for it. Same per-join rule as AshwoodEntitySorter
+    private static boolean isJoinedToPK(DbEntity entity, DbAttribute attribute) {
+        for (DbRelationship relationship : entity.getRelationships()) {
+            if (relationship.isToMany() || relationship.isToDependentPK()) {
+                continue;
+            }
+            for (DbJoin join : relationship.getJoins()) {
+                if (join.getSource() == attribute && join.getTarget() != null && join.getTarget().isPrimaryKey()) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
     private void createReverseRelationship(DbEntity eSource, DbEntity eTarget) {
         InferredRelationship myir = new InferredRelationship();
         for (DbRelationship relationship : eSource.getRelationships()) {
diff --git a/cayenne/src/main/java/org/apache/cayenne/map/DbAttribute.java b/cayenne/src/main/java/org/apache/cayenne/map/DbAttribute.java
index 76fd05449..17babe97a 100644
--- a/cayenne/src/main/java/org/apache/cayenne/map/DbAttribute.java
+++ b/cayenne/src/main/java/org/apache/cayenne/map/DbAttribute.java
@@ -157,33 +157,6 @@ public class DbAttribute extends Attribute<DbEntity, DbAttribute, DbRelationship
         return primaryKey;
     }
 
-    /**
-     * Returns <code>true</code> if the DB column represented by this attribute
-     * is a foreign key, referencing another table.
-     * 
-     * @since 1.1
-     */
-    public boolean isForeignKey() {
-        String name = getName();
-        if (name == null) {
-            // won't be able to match joins...
-            return false;
-        }
-
-        for (DbRelationship relationship : getEntity().getRelationships()) {
-            for (DbJoin join : relationship.getJoins()) {
-                if (name.equals(join.getSourceName())) {
-                    DbAttribute target = join.getTarget();
-                    if (target != null && target.isPrimaryKey()) {
-                        return true;
-                    }
-                }
-            }
-        }
-
-        return false;
-    }
-
     /**
      * Updates attribute "primaryKey" property.
      */
diff --git a/cayenne/src/test/java/org/apache/cayenne/unit/DbSchemaManager.java b/cayenne/src/test/java/org/apache/cayenne/unit/DbSchemaManager.java
index f64f723de..36b0df679 100644
--- a/cayenne/src/test/java/org/apache/cayenne/unit/DbSchemaManager.java
+++ b/cayenne/src/test/java/org/apache/cayenne/unit/DbSchemaManager.java
@@ -26,6 +26,7 @@ import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbJoin;
 import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.Procedure;
 import org.apache.cayenne.CayenneRuntime;
@@ -119,6 +120,30 @@ public class DbSchemaManager {
         }
     }
 
+    private static final Set<Integer> BINARY_TYPES = Set.of(Types.BINARY, Types.VARBINARY, Types.LONGVARBINARY);
+
+    // a table can't exist on a DB without binary PK support if its own PK is binary, or if one of its to-one
+    // relationships joins to a binary PK (the join column has the PK type). Same per-join rule as AshwoodEntitySorter
+    private static boolean hasBinaryKey(DbEntity entity) {
+        for (DbAttribute pk : entity.getPrimaryKeys()) {
+            if (BINARY_TYPES.contains(pk.getType())) {
+                return true;
+            }
+        }
+        for (DbRelationship relationship : entity.getRelationships()) {
+            if (relationship.isToMany() || relationship.isToDependentPK()) {
+                continue;
+            }
+            for (DbJoin join : relationship.getJoins()) {
+                DbAttribute target = join.getTarget();
+                if (target != null && target.isPrimaryKey() && BINARY_TYPES.contains(target.getType())) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
     private void filterDataMap(DataMap map) {
         boolean supportsBinaryPK = testDbAdapter.supportsBinaryPK();
 
@@ -129,15 +154,8 @@ public class DbSchemaManager {
         List<DbEntity> entitiesToRemove = new ArrayList<>();
 
         for (DbEntity ent : map.getDbEntities()) {
-            for (DbAttribute attr : ent.getAttributes()) {
-                // check for BIN PK or FK to BIN Pk
-                if (attr.getType() == Types.BINARY || attr.getType() == Types.VARBINARY
-                        || attr.getType() == Types.LONGVARBINARY) {
-                    if (attr.isPrimaryKey() || attr.isForeignKey()) {
-                        entitiesToRemove.add(ent);
-                        break;
-                    }
-                }
+            if (hasBinaryKey(ent)) {
+                entitiesToRemove.add(ent);
             }
         }
 
@@ -334,16 +352,8 @@ public class DbSchemaManager {
                 }
             }
 
-            // check for BIN PK
-            if (excludeBinaryPK) {
-                Set<Integer> binaryTypes = Set.of(Types.BINARY, Types.VARBINARY, Types.LONGVARBINARY);
-                boolean hasBinaryPK = entity.getAttributes().stream()
-                        .filter(attribute -> attribute.isPrimaryKey() || attribute.isForeignKey())
-                        .map(DbAttribute::getType)
-                        .anyMatch(binaryTypes::contains);
-                if (hasBinaryPK) {
-                    excludedEntities.add(entity);
-                }
+            if (excludeBinaryPK && hasBinaryKey(entity)) {
+                excludedEntities.add(entity);
             }
         }
         return excludedEntities;