@@ -1075,9 +1075,17 @@ public String[] generateSchemaCreationScript(Dialect dialect) throws HibernateEx
1075
1075
)
1076
1076
);
1077
1077
}
1078
+ }
1079
+ }
1080
+
1081
+ // Foreign keys must be created *after* unique keys for numerous DBs. See HH-8390.
1082
+ iter = getTableMappings ();
1083
+ while ( iter .hasNext () ) {
1084
+ Table table = (Table ) iter .next ();
1085
+ if ( table .isPhysicalTable () ) {
1078
1086
1079
1087
if ( dialect .hasAlterTable () ) {
1080
- subIter = table .getForeignKeyIterator ();
1088
+ Iterator subIter = table .getForeignKeyIterator ();
1081
1089
while ( subIter .hasNext () ) {
1082
1090
ForeignKey fk = (ForeignKey ) subIter .next ();
1083
1091
if ( fk .isPhysicalConstraint () ) {
@@ -1215,6 +1223,33 @@ public List<SchemaUpdateScript> generateSchemaUpdateScriptList(Dialect dialect,
1215
1223
}
1216
1224
}
1217
1225
1226
+ Iterator subIter = table .getIndexIterator ();
1227
+ while ( subIter .hasNext () ) {
1228
+ final Index index = (Index ) subIter .next ();
1229
+ // Skip if index already exists
1230
+ if ( tableInfo != null && StringHelper .isNotEmpty ( index .getName () ) ) {
1231
+ final IndexMetadata meta = tableInfo .getIndexMetadata ( index .getName () );
1232
+ if ( meta != null ) {
1233
+ continue ;
1234
+ }
1235
+ }
1236
+ scripts .add ( new SchemaUpdateScript ( index .sqlCreateString ( dialect , mapping , tableCatalog ,
1237
+ tableSchema ), false ) );
1238
+ }
1239
+ }
1240
+ }
1241
+
1242
+ // Foreign keys must be created *after* unique keys for numerous DBs. See HH-8390.
1243
+ iter = getTableMappings ();
1244
+ while ( iter .hasNext () ) {
1245
+ Table table = (Table ) iter .next ();
1246
+ String tableSchema = ( table .getSchema () == null ) ? defaultSchema : table .getSchema ();
1247
+ String tableCatalog = ( table .getCatalog () == null ) ? defaultCatalog : table .getCatalog ();
1248
+ if ( table .isPhysicalTable () ) {
1249
+
1250
+ TableMetadata tableInfo = databaseMetadata .getTableMetadata ( table .getName (), tableSchema ,
1251
+ tableCatalog , table .isQuoted () );
1252
+
1218
1253
if ( dialect .hasAlterTable () ) {
1219
1254
Iterator subIter = table .getForeignKeyIterator ();
1220
1255
while ( subIter .hasNext () ) {
@@ -1230,20 +1265,6 @@ public List<SchemaUpdateScript> generateSchemaUpdateScriptList(Dialect dialect,
1230
1265
}
1231
1266
}
1232
1267
}
1233
-
1234
- Iterator subIter = table .getIndexIterator ();
1235
- while ( subIter .hasNext () ) {
1236
- final Index index = (Index ) subIter .next ();
1237
- // Skip if index already exists
1238
- if ( tableInfo != null && StringHelper .isNotEmpty ( index .getName () ) ) {
1239
- final IndexMetadata meta = tableInfo .getIndexMetadata ( index .getName () );
1240
- if ( meta != null ) {
1241
- continue ;
1242
- }
1243
- }
1244
- scripts .add ( new SchemaUpdateScript ( index .sqlCreateString ( dialect , mapping , tableCatalog ,
1245
- tableSchema ), false ) );
1246
- }
1247
1268
}
1248
1269
}
1249
1270
0 commit comments