Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix foreign key creation in PostgreSQL
  • Loading branch information
alekseinovikov committed Jun 1, 2020
1 parent d5c4850 commit 16ddad5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class JdbcDatabaseMetadataImpl(database: String, val metadata: DatabaseMetaData)
val fromTableName = getString("FKTABLE_NAME")!!
val fromColumnName = identifierManager.quoteIdentifierWhenWrongCaseOrNecessary(getString("FKCOLUMN_NAME")!!)
val fromColumn = allTables.getValue(fromTableName).columns.firstOrNull {
identifierManager.quoteIdentifierWhenWrongCaseOrNecessary(it.nameInDatabaseCase()) == fromColumnName
identifierManager.quoteIdentifierWhenWrongCaseOrNecessary(it.name) == fromColumnName
} ?: return@iterate null // Do not crash if there are missing fields in Exposed's tables
val constraintName = getString("FK_NAME")!!
val targetTableName = getString("PKTABLE_NAME")!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ class CreateMissingTablesAndColumnsTests : DatabaseTestsBase() {
}
}

@Test
fun testForeignKeyCreationInPostgres() {
val usersTable = object : IntIdTable("users") {}
val spacesTable = object : IntIdTable("spaces") {
val userId = reference("userId", usersTable)
}

withDb(TestDB.POSTGRESQL) {
SchemaUtils.createMissingTablesAndColumns(usersTable, spacesTable)
}
}

object MultipleIndexesTable: Table("H2_MULTIPLE_INDEXES") {
val value1 = varchar("value1", 255)
val value2 = varchar("value2", 255)
Expand Down

0 comments on commit 16ddad5

Please sign in to comment.