DatabaseField fkField = null;
DatabaseField targetField = null;
Vector<String> fkFieldNames = new Vector();
Vector<String> targetFieldNames = new Vector();
DatabaseTable sourceTable = fkFields.get(0).getTable();
TableDefinition sourceTableDef = getTableDefFromDBTable(sourceTable);
for (int i=0; i < fkFields.size(); i++) {
fkField = fkFields.get(i);
targetField = targetFields.get(i);
fkFieldNames.add(fkField.getName());
targetFieldNames.add(targetField.getName());
FieldDefinition fkFieldDef = fieldMap.get(fkField);
FieldDefinition targetFieldDef = fieldMap.get(targetField);
if (targetFieldDef != null) {
// UnidirectionalOneToOneMapping case
if (fkFieldDef == null) {
fkFieldDef = getFieldDefFromDBField(fkField, false);
if (!sourceTableDef.getFields().contains(fkFieldDef)) {
sourceTableDef.addField(fkFieldDef);
}
}
// Also ensure that the type, size and subsize of the foreign key field is
// same as that of the original field.
fkFieldDef.setType(targetFieldDef.getType());
fkFieldDef.setSize(targetFieldDef.getSize());
fkFieldDef.setSubSize(targetFieldDef.getSubSize());
}
}
// add a foreign key constraint
DatabaseTable targetTable = targetField.getTable();
TableDefinition targetTableDef = getTableDefFromDBTable(targetTable);
addForeignKeyConstraint(sourceTableDef, targetTableDef, fkFieldNames, targetFieldNames);
}