Exposed icon indicating copy to clipboard operation
Exposed copied to clipboard

Requests transpiled to duplicate SQL queries.

Open sergey-bulavskiy opened this issue 2 years ago • 0 comments

Hello!

Dependencies:

exposedVersion=0.38.1
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-dao:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-jdbc:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-java-time:$exposedVersion")
implementation("org.jetbrains.exposed:exposed-java-time:$exposedVersion")

I have a simple transaction:

return transaction {
            val nowLocal = now.toLocalDateTime()
            var user = User.find { Users.telegramId eq telegramId }.single()

            user.firstName = firstName
            user.lastName = lastName
            user.nickName = nickName
            user.lastMessagedAt = nowLocal
        }

For some reason it transpiles to this SQL:

18:44:42.233 [---] DEBUG Exposed - SELECT users.id, users.nick_name, users.first_name, users.last_name, users.telegram_id, users.rating, users.created_at, users.updated_at, users.last_message_at, users.last_updated_rating_at FROM users WHERE users.telegram_id = 133824122
18:44:42.233 [---] DEBUG Exposed - SELECT users.id, users.nick_name, users.first_name, users.last_name, users.telegram_id, users.rating, users.created_at, users.updated_at, users.last_message_at, users.last_updated_rating_at FROM users WHERE users.telegram_id = 133824122
18:44:42.544 [---] DEBUG Exposed - UPDATE users SET last_message_at='2022-05-02T11:44:42.1992965' WHERE id = 2
18:44:42.544 [---] DEBUG Exposed - UPDATE users SET last_message_at='2022-05-02T11:44:42.1992965' WHERE id = 2

I can imagine that i have two selects because of some kind of virtualization (first on .find and second on .single(), for example) but why does update happen two times for me is not clear. Maybe it is a logger bug ?

I init database like this:

Database.connect(connect())
...

private fun connect(): HikariDataSource {
    val config = HikariConfig()
    config.jdbcUrl = dbConfiguration.dbUrl
    config.username = dbConfiguration.dbUsername
    config.password = dbConfiguration.dbPassword
    config.driverClassName = "org.postgresql.Driver"

    return HikariDataSource(config)
}

Please advise, maybe i'm doing something wrong?

sergey-bulavskiy avatar May 02 '22 11:05 sergey-bulavskiy