diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt index 950401f84e..0a1f4a241f 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Constraints.kt @@ -106,7 +106,14 @@ data class ForeignKeyConstraint( /** Name of this constraint. */ val fkName: String get() = tx.db.identifierManager.cutIfNecessaryAndQuote( - name ?: "fk_${fromTable.tableNameWithoutScheme}_${from.joinToString("_") { it.name }}__${target.joinToString("_") { it.name }}" + name ?: "fk_${ + fromTable.tableNameWithoutScheme + // Table name may contain quotes, remove those before appending + .replace( + "\"", + "" + ) + }_${from.joinToString("_") { it.name }}__${target.joinToString("_") { it.name }}" ).inProperCase() internal val foreignKeyPart: String get() = buildString { diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt index 7644135f07..12a863805f 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/ddl/CreateTableTests.kt @@ -219,6 +219,31 @@ class CreateTableTests : DatabaseTestsBase() { } } + @Test + fun createTableWithQuotes() { + val parent = object : LongIdTable("\"Parent\"") {} + val child = object : LongIdTable("\"Child\"") { + val parentId = reference( + name = "parent_id", + foreign = parent, + onUpdate = ReferenceOption.NO_ACTION, + onDelete = ReferenceOption.NO_ACTION, + ) + } + withTables(parent, child) { + // Different dialects use different mix of lowercase/uppercase in their names + val expected = listOf( + "CREATE TABLE " + addIfNotExistsIfSupported() + "${this.identity(child)} (" + + "${child.columns.joinToString { it.descriptionDdl(false) }}," + + " CONSTRAINT ${"fk_Child_parent_id__id".inProperCase()}" + + " FOREIGN KEY (${this.identity(child.parentId)})" + + " REFERENCES ${this.identity(parent)}(${this.identity(parent.id)})" + + ")" + ) + assertEqualCollections(child.ddl, expected) + } + } + @Test fun createTableWithExplicitForeignKeyName2() { val fkName = "MyForeignKey2"