HHH-10425 - Fix SchemaMigration creates foreign keys too early if across schema

This commit is contained in:
Andrea Boriero 2016-01-07 14:30:31 +00:00
parent df362e4568
commit 3fb9577e50
1 changed files with 11 additions and 1 deletions

View File

@ -183,7 +183,6 @@ public class SchemaMigratorImpl implements SchemaMigrator {
applyIndexes( table, tableInformation, metadata, targets );
applyUniqueKeys( table, tableInformation, metadata, targets );
applyForeignKeys( table, tableInformation, metadata, targets );
}
for ( Sequence sequence : namespace.getSequences() ) {
@ -205,6 +204,17 @@ public class SchemaMigratorImpl implements SchemaMigrator {
}
}
//NOTE : Foreign keys must be created *after* all tables of all namespaces for cross namespace fks. see HHH-10420
for ( Namespace namespace : database.getNamespaces() ) {
for ( Table table : namespace.getTables() ) {
final TableInformation tableInformation = existingDatabase.getTableInformation( table.getQualifiedTableName() );
if ( tableInformation != null && !tableInformation.isPhysicalTable() ) {
continue;
}
applyForeignKeys( table, tableInformation, metadata, targets );
}
}
// Create after-table AuxiliaryDatabaseObjects
for ( AuxiliaryDatabaseObject auxiliaryDatabaseObject : database.getAuxiliaryDatabaseObjects() ) {
if ( !auxiliaryDatabaseObject.beforeTablesOnCreation() ) {