From cc90586aa0020b822c4ca75bdd6e8f2a06bc869e Mon Sep 17 00:00:00 2001 From: hengsin Date: Thu, 11 May 2023 18:43:00 +0800 Subject: [PATCH] IDEMPIERE-5717 MColumn.getForeignKeyConstraint doesn't always generate unique foreign key constraint name (#1830) --- .../src/org/compiere/model/MColumn.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/org.adempiere.base/src/org/compiere/model/MColumn.java b/org.adempiere.base/src/org/compiere/model/MColumn.java index 7c74f9f15a..412fe08bb0 100644 --- a/org.adempiere.base/src/org/compiere/model/MColumn.java +++ b/org.adempiere.base/src/org/compiere/model/MColumn.java @@ -1184,6 +1184,18 @@ public class MColumn extends X_AD_Column implements ImmutablePOSupport if (constraintName.length() > 30) constraintName = new StringBuilder(constraintName.substring(0, 30)); fkConstraintName = constraintName.toString(); + + int duplicateId = DB.getSQLValueEx(column.get_TrxName(), "SELECT AD_Column_ID FROM AD_Column WHERE Upper(FkConstraintName)=?", fkConstraintName.toUpperCase()); + int loop = 0; + while (duplicateId > 0) + { + loop++; + String suffix = "" + loop; + if (fkConstraintName.length() + suffix.length() > 30) + fkConstraintName = fkConstraintName.substring(0, fkConstraintName.length() - (fkConstraintName.length() + suffix.length() - 30)); + fkConstraintName = fkConstraintName + loop; + duplicateId = DB.getSQLValueEx(column.get_TrxName(), "SELECT AD_Column_ID FROM AD_Column WHERE Upper(FkConstraintName)=?", fkConstraintName.toUpperCase()); + } } StringBuilder fkConstraint = new StringBuilder();