diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt index 6592423b7d..30014fd7fe 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt @@ -24,9 +24,7 @@ object SchemaUtils { emptyMap() } else { tables.associateWith { t -> - t.columns.mapNotNull { c -> - c.referee?.let { it.table to c.columnType.nullable } - }.toMap() + t.foreignKeys.map { it.targetTable } } } } @@ -36,9 +34,7 @@ object SchemaUtils { fun parseTable(table: Table) { if (result.add(table)) { - table.columns.forEach { - it.referee?.table?.let(::parseTable) - } + table.foreignKeys.map { it.targetTable }.forEach(::parseTable) } } tables.forEach(::parseTable) @@ -54,7 +50,7 @@ object SchemaUtils { fun traverse(table: Table) { if (table !in visited) { visited += table - graph.getValue(table).forEach { (t, _) -> + graph.getValue(table).forEach { t -> if (t !in visited) { traverse(t) } @@ -79,7 +75,7 @@ object SchemaUtils { if (table in visited) return false recursion += table visited += table - return if (graph[table]!!.any { traverse(it.key) }) { + return if (graph[table]!!.any { traverse(it) }) { true } else { recursion -= table diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateMissingTablesAndColumnsTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateMissingTablesAndColumnsTests.kt index 65db329aa9..313ac950b8 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateMissingTablesAndColumnsTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateMissingTablesAndColumnsTests.kt @@ -20,7 +20,7 @@ import org.jetbrains.exposed.sql.vendors.OracleDialect import org.jetbrains.exposed.sql.vendors.PrimaryKeyMetadata import org.junit.Test import java.math.BigDecimal -import java.util.* +import java.util.UUID import kotlin.properties.Delegates import kotlin.test.assertNotNull import kotlin.test.assertNull @@ -539,7 +539,7 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() { uniqueIndex("index2", value2, value1) } } - + @Test fun testCreateTableWithReferenceMultipleTimes() { withTables(PlayerTable, SessionsTable) { @@ -652,6 +652,22 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() { } } + @Test + fun testCreateCompositePrimaryKeyTableAndCompositeForeignKeyInVariousOrder() { + withTables(CompositeForeignKeyTable, CompositePrimaryKeyTable) { + SchemaUtils.createMissingTablesAndColumns(CompositePrimaryKeyTable, CompositeForeignKeyTable) + } + withTables(CompositeForeignKeyTable, CompositePrimaryKeyTable) { + SchemaUtils.createMissingTablesAndColumns(CompositeForeignKeyTable, CompositePrimaryKeyTable) + } + withTables(CompositePrimaryKeyTable, CompositeForeignKeyTable) { + SchemaUtils.createMissingTablesAndColumns(CompositePrimaryKeyTable, CompositeForeignKeyTable) + } + withTables(CompositePrimaryKeyTable, CompositeForeignKeyTable) { + SchemaUtils.createMissingTablesAndColumns(CompositeForeignKeyTable, CompositePrimaryKeyTable) + } + } + @Test fun testCreateTableWithSchemaPrefix() { val schemaName = "my_schema"