Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With quoted table names SchemaUtils create stops working. #1550

Closed
miladamery opened this issue Jul 12, 2022 · 0 comments · Fixed by #1562
Closed

With quoted table names SchemaUtils create stops working. #1550

miladamery opened this issue Jul 12, 2022 · 0 comments · Fixed by #1562

Comments

@miladamery
Copy link

miladamery commented Jul 12, 2022

Hi.
I want to have capitalize table names in my postgresql database. Postgresql change names to lowercase unless they are quoted but with quoted table names in kotlin exposed 2 problems happen.

  1. Constraint name generation goes wrong
    When quoting my table name like:
object CustomerTable : IntIdTable("\"Customer\"")

constraints will be generated like below
CONSTRAINT "fk_"Customer"_userId_id" FOREIGN KEY ("userId") REFERENCES "User"(id)
and database will complain about this constraint name.

  1. Table existence check when generating table doesn't work
    Constraint name problem can be by passed with giving name to all constraints yourself. But in SchemaUtils.createStatements method line
val toCreate = sortTablesByReferences(tables.toList()).filterNot { it.exists() }

won't work correctly. Because it.exists() is:

// 
fun Table.exists(): Boolean = currentDialect.tableExists(this)

// Implementation in VendorDialect
override fun tableExists(table: Table): Boolean {
        val tableScheme = table.tableName.substringBefore('.', "").takeIf { it.isNotEmpty() }
        val scheme = tableScheme?.inProperCase() ?: TransactionManager.current().connection.metadata { currentScheme }
        val allTables = getAllTableNamesCache().getValue(scheme)
        return allTables.any {
            when {
                tableScheme != null -> it == table.nameInDatabaseCase()
                scheme.isEmpty() -> it == table.nameInDatabaseCase()
                else -> it == "$scheme.${table.tableNameWithoutScheme}".inProperCase()
            }
        }
    }

now imagine having a table "User". in the else part 'it' value is "public.user" and result of
"$scheme.${table.tableNameWithoutScheme}".inProperCase() is "public."user"". these 2 are not equal and result of it.exists() is always false. Then a single table comes for generation many times and database will complain about constraints that are generated before and already exist in database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant