Exposed: [Bug] Regression in 0.22.1, createMissingTablesAndColumns fails

Created on 26 Mar 2020  路  7Comments  路  Source: JetBrains/Exposed

The code below works with exposed 0.21.1, but fails with 0.22.1.

package org.user5145.champion.integration

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
import kotlin.test.Test

class BugTest {
    fun getDefaultDb(): Database {
        val config = HikariConfig()
        config.jdbcUrl = "jdbc:h2:mem:test;MODE=Postgresql;DB_CLOSE_DELAY=-1;DATABASE_TO_LOWER=TRUE"
        config.username = ""
        config.password = ""
        config.driverClassName = "org.h2.Driver"
        return Database.connect(HikariDataSource(config))
    }

    object Champions : IdTable<String>() {
        override val id get() = username
        val username = varchar("name", 50).entityId()
        val pass = text("pass")
    }

    object ChampionsStats : IdTable<String>("champions_stats") {
        override val id get() = username
        val username = varchar("name", 50).entityId() references Champions.username
        val hp = text("hp")
    }

    @Test
    fun testCreate() {
        transaction(getDefaultDb()) {
            SchemaUtils.createMissingTablesAndColumns(Champions, ChampionsStats)
        }
    }
}

Stacktrace head:

Collection contains no element matching the predicate.
java.util.NoSuchElementException: Collection contains no element matching the predicate.
    at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImpl$tableConstraints$$inlined$associateWith$lambda$1.invoke(JdbcDatabaseMetadataImpl.kt:170)
    at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImpl$tableConstraints$$inlined$associateWith$lambda$1.invoke(JdbcDatabaseMetadataImpl.kt:13)
    at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImplKt.iterate(JdbcDatabaseMetadataImpl.kt:164)
    at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImpl.tableConstraints(JdbcDatabaseMetadataImpl.kt:123)
bug

All 7 comments

We are getting the same error

Exception in thread "main" java.util.NoSuchElementException: Collection contains no element matching the predicate.
        at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImpl$tableConstraints$$inlined$associateWith$lambda$1.invoke(JdbcDatabaseMetadataImpl.kt:170)
        at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImpl$tableConstraints$$inlined$associateWith$lambda$1.invoke(JdbcDatabaseMetadataImpl.kt:13)
        at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImplKt.iterate(JdbcDatabaseMetadataImpl.kt:164)
        at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImpl.tableConstraints(JdbcDatabaseMetadataImpl.kt:123)
        at org.jetbrains.exposed.sql.vendors.VendorDialect$fillConstraintCacheForTables$1.invoke(Default.kt:639)
        at org.jetbrains.exposed.sql.vendors.VendorDialect$fillConstraintCacheForTables$1.invoke(Default.kt:560)
        at org.jetbrains.exposed.sql.statements.jdbc.JdbcConnectionImpl.metadata(JdbcConnectionImpl.kt:47)
        at org.jetbrains.exposed.sql.Database.metadata$exposed_core(Database.kt:31)
        at org.jetbrains.exposed.sql.vendors.VendorDialect.fillConstraintCacheForTables(Default.kt:639)
        at org.jetbrains.exposed.sql.vendors.VendorDialect.columnConstraints(Default.kt:617)
        at org.jetbrains.exposed.sql.SchemaUtils.addMissingColumnsStatements(SchemaUtils.kt:145)
        at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns(SchemaUtils.kt:241)
        at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns$default(SchemaUtils.kt:229

0.22.1

Same here. Working in 0.21.1 but not in 0.23.1

@user5145 , I was not able to reproduce your exception.
Please specify on which h2 version do you run your code and try to check the latest Exposed 0.23.1 (looks like a one of #839 or #843).
Also, please try to replace

val username = varchar("name", 50).entityId() references Champions.username
with
val username = reference("name", Champions)

@baptistecassar, could you share a sample, please?

Works fine in 0.23.1 for me, but I will keep the issue for @baptistecassar

@Tapac changing

varchar("name", 50).entityId() references Champions.username
to
reference("name", Champions)

doesn't do much.
If you still want a sample, here

@baptistecassar I've been getting this error in 0.23.1 while using postgres, but it was caused by gradle cache and "Reimport all gradle projects" button helped.
image

Thanks for your help @Tapac :)

I had the same exception when using 0.23.1. Turned out, that it happens when you try to define a reference with non-lowercase name.

This doesn't work:
val revisionId: Column<Long> = reference("revisionId", RevisionTable.id)

This is fine:
val revisionId: Column<Long> = reference("revisionid", RevisionTable.id)

Also it only happens when using postgres.

This issue still happens in 0.24.1 if you do this (using PostgreSQL):

  1. Create a table object with a external table reference (Example: var myExternalReference = optReference("my_external_reference", MyExternalTable, onDelete = ReferenceOption.CASCADE).index())
  2. Create the table with SchemaUtils.createMissingTablesAndColumns
  3. Remove the reference from the table in your code
  4. Use SchemaUtils.createMissingTablesAndColumns again
  5. Exposed fails to create the table... even tho it is already created

Then the error happens, this doesn't happen with 0.17.7 (which was the previous Exposed version I was using before I upgraded)

Not sure if this is intentional because sometimes you don't want to map everything from your database table to Exposed (in my case sometimes I remove features from my app that I didn't remove from the database schema yet, in 0.17.7 it worked fine... in 0.24.1 it breaks)

@Tapac should a new issue be created about this issue, or does it fit in here too? The stacktrace is the same as the one in this issue.

Exception in thread "main" java.util.NoSuchElementException: Collection contains no element matching the predicate.
        at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImpl$tableConstraints$$inlined$associateWith$lambda$1.invoke(JdbcDatabaseMetadataImpl.kt:213)
        at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImpl$tableConstraints$$inlined$associateWith$lambda$1.invoke(JdbcDatabaseMetadataImpl.kt:13)
        at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImplKt.iterate(JdbcDatabaseMetadataImpl.kt:206)
        at org.jetbrains.exposed.sql.statements.jdbc.JdbcDatabaseMetadataImpl.tableConstraints(JdbcDatabaseMetadataImpl.kt:170)
        at org.jetbrains.exposed.sql.vendors.VendorDialect$fillConstraintCacheForTables$1.invoke(Default.kt:677)
        at org.jetbrains.exposed.sql.vendors.VendorDialect$fillConstraintCacheForTables$1.invoke(Default.kt:584)
        at org.jetbrains.exposed.sql.statements.jdbc.JdbcConnectionImpl.metadata(JdbcConnectionImpl.kt:51)
        at org.jetbrains.exposed.sql.Database.metadata$exposed_core(Database.kt:29)
        at org.jetbrains.exposed.sql.vendors.VendorDialect.fillConstraintCacheForTables(Default.kt:677)
        at org.jetbrains.exposed.sql.vendors.VendorDialect.columnConstraints(Default.kt:655)
        at org.jetbrains.exposed.sql.SchemaUtils.addMissingColumnsStatements(SchemaUtils.kt:144)
        at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns(SchemaUtils.kt:240)
        at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns$default(SchemaUtils.kt:228)
        at com.mrpowergamerbr.loritta.Loritta$initPostgreSql$1.invoke(Loritta.kt:389)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

yuri-li picture yuri-li  路  3Comments

vasily-kirichenko picture vasily-kirichenko  路  4Comments

gertvdijk picture gertvdijk  路  4Comments

kszymanski85 picture kszymanski85  路  4Comments

hannesstruss picture hannesstruss  路  5Comments