// go through each relation to see if an FK already exists
Iterator<ModelRelation> relations = entity.getRelationsIterator();
boolean createdConstraints = false;
while (relations.hasNext()) {
ModelRelation modelRelation = relations.next();
if (!"one".equals(modelRelation.getType())) {
continue;
}
ModelEntity relModelEntity = modelEntities.get(modelRelation.getRelEntityName());
if (relModelEntity == null) {
Debug.logError("No such relation: " + entity.getEntityName() + " -> " + modelRelation.getRelEntityName(), module);
continue;
}
String relConstraintName = makeFkConstraintName(modelRelation, datasourceInfo.constraintNameClipLength);
ReferenceCheckInfo rcInfo = null;
if (rcInfoMap != null) {
rcInfo = rcInfoMap.get(relConstraintName);
}
if (rcInfo != null) {
rcInfoMap.remove(relConstraintName);
} else {
// if not, create one
String noFkMessage = "No Foreign Key Constraint [" + relConstraintName + "] found for entity [" + entityName + "]";
if (messages != null) messages.add(noFkMessage);
if (Debug.infoOn()) Debug.logInfo(noFkMessage, module);
if (addMissing) {
String errMsg = createForeignKey(entity, modelRelation, relModelEntity, datasourceInfo.constraintNameClipLength, datasourceInfo.fkStyle, datasourceInfo.useFkInitiallyDeferred);
if (UtilValidate.isNotEmpty(errMsg)) {
String message = "Could not create foreign key " + relConstraintName + " for entity [" + entity.getEntityName() + "]: " + errMsg;
Debug.logError(message, module);
if (messages != null) messages.add(message);
} else {
String message = "Created foreign key " + relConstraintName + " for entity [" + entity.getEntityName() + "]";
Debug.logVerbose(message, module);
if (messages != null) messages.add(message);
createdConstraints = true;
numFksCreated++;
}
}
}
}
if (createdConstraints) {
String message = "Created foreign key(s) for entity [" + entity.getEntityName() + "]";
Debug.logImportant(message, module);
if (messages != null) messages.add(message);
}
// show foreign key references that exist but are unknown
if (rcInfoMap != null) {
for (String rcKeyLeft: rcInfoMap.keySet()) {
String message = "Unknown Foreign Key Constraint " + rcKeyLeft + " found in table " + entity.getTableName(datasourceInfo);
Debug.logImportant(message, module);
if (messages != null) messages.add(message);
}
}
}
}
if (Debug.infoOn()) Debug.logInfo("Created " + numFksCreated + " fk refs", module);
}
// make sure each one-relation has an index
if (checkFkIdx || datasourceInfo.checkIndicesOnStart) {
//if (!justColumns && datasourceInfo.useFkIndices && datasourceInfo.checkFkIndicesOnStart) {
int numIndicesCreated = 0;
// TODO: check each key-map to make sure it exists in the index, if any differences warn and then remove the index and recreate it
// get ALL column info, put into hashmap by table name
boolean needsUpperCase[] = new boolean[1];
Map<String, Set<String>> tableIndexListMap = this.getIndexInfo(indexTableNames, messages, needsUpperCase);
// Debug.logVerbose("Ref Info Map: " + refTableInfoMap, module);
if (tableIndexListMap == null) {
// uh oh, something happened while getting info...
if (Debug.verboseOn()) Debug.logVerbose("Ref Table Info Map is null", module);
} else {
for (ModelEntity entity: modelEntityList) {
String entityName = entity.getEntityName();
// if this is a view entity, do not check it...
if (entity instanceof ModelViewEntity) {
String entMessage = "NOT Checking View Entity " + entity.getEntityName();
Debug.logVerbose(entMessage, module);
if (messages != null) messages.add(entMessage);
continue;
}
// get existing index list for this table
Set<String> tableIndexList = tableIndexListMap.get(entity.getTableName(datasourceInfo));
// Debug.logVerbose("Got ind info for table " + entity.getTableName(datasourceInfo) + ": " + tableIndexList, module);
if (tableIndexList == null) {
// evidently no indexes in the database for this table, do the create all
if (checkFkIdx) {
this.createForeignKeyIndices(entity, datasourceInfo.constraintNameClipLength, messages);
}
if (datasourceInfo.checkIndicesOnStart) {
this.createDeclaredIndices(entity, messages);
}
continue;
}
// go through each relation to see if an FK already exists
boolean createdConstraints = false;
Iterator<ModelRelation> relations = entity.getRelationsIterator();
while (relations.hasNext()) {
ModelRelation modelRelation = relations.next();
if (!"one".equals(modelRelation.getType())) {
continue;
}
String relConstraintName = makeFkConstraintName(modelRelation, datasourceInfo.constraintNameClipLength);
if (tableIndexList.contains(relConstraintName)) {