#1834 - DDL for ElementCollection that is referenced multiple times includes foreign key constraint

This commit is contained in:
rob bygrave
2019-10-08 15:53:29 +13:00
parent 7bc6368f69
commit 5e7dda1ec8
9 changed files with 265 additions and 9 deletions
@@ -261,6 +261,13 @@ public class MColumn {
return !draftOnly && !historyExclude;
}
public void clearForeignKey() {
this.references = null;
this.foreignKeyName = null;
this.fkeyOnDelete = null;
this.fkeyOnUpdate = null;
}
public Column createColumn() {
Column c = new Column();
@@ -273,8 +273,7 @@ public class MTable {
}
for (MCompoundUniqueConstraint constraint : uniqueConstraints) {
UniqueConstraint uq = constraint.getUniqueConstraint();
createTable.getUniqueConstraint().add(uq);
createTable.getUniqueConstraint().add(constraint.getUniqueConstraint());
}
return createTable;
@@ -789,4 +788,20 @@ public class MTable {
public void setPartitionMeta(PartitionMeta partitionMeta) {
this.partitionMeta = partitionMeta;
}
/**
* Clear foreign key as this element collection table logically references
* back to multiple tables.
*/
public MIndex setReusedElementCollection() {
MIndex index = null;
for (MColumn column : columns.values()) {
final String references = column.getReferences();
if (references != null) {
index = new MIndex(column.getForeignKeyIndex(), name, column.getName());
column.clearForeignKey();
}
}
return index;
}
}
@@ -312,11 +312,29 @@ public class ModelContainer {
return tables.put(table.getName(), table);
}
/**
* Add an element table taking into account if it is reused/references back
* to multiple bean types (and so can't have foreign key).
*/
public void addTableElementCollection(MTable table) {
final MTable reusedElementCollection = tables.get(table.getName());
if (reusedElementCollection != null) {
final MIndex index = reusedElementCollection.setReusedElementCollection();
if (index != null) {
indexes.put(index.getIndexName(), index);
}
} else {
if (table.isPartitioned()) {
partitionedTables.add(table);
}
tables.put(table.getName(), table);
}
}
/**
* Add a single column index.
*/
public void addIndex(String indexName, String tableName, String columnName) {
indexes.put(indexName, new MIndex(indexName, tableName, columnName));
}
@@ -324,7 +342,6 @@ public class ModelContainer {
* Add a multi column index.
*/
public void addIndex(String indexName, String tableName, String[] columnNames) {
indexes.put(indexName, new MIndex(indexName, tableName, columnNames));
}
@@ -95,6 +95,10 @@ public class ModelBuildContext {
return model.addTable(table);
}
public void addTableElementCollection(MTable table) {
model.addTableElementCollection(table);
}
public void addIndex(String indexName, String tableName, String columnName) {
model.addIndex(indexName, tableName, columnName);
}
@@ -17,15 +17,11 @@ public class ModelBuildElementTable {
public static void build(ModelBuildContext ctx, BeanPropertyAssocMany<?> manyProp) {
BeanTable beanTable = manyProp.getBeanTable();
BeanDescriptor<?> targetDescriptor = manyProp.getTargetDescriptor();
MTable table = new MTable(beanTable.getBaseTable());
VisitAllUsing.visitOne(targetDescriptor, new ModelBuildPropertyVisitor(ctx, table, targetDescriptor));
ctx.addTable(table);
ctx.addTableElementCollection(table);
}
}