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

Batch insert throws irrelevant exception #741

Closed
xJoeWoo opened this issue Dec 26, 2019 · 2 comments
Closed

Batch insert throws irrelevant exception #741

xJoeWoo opened this issue Dec 26, 2019 · 2 comments

Comments

@xJoeWoo
Copy link
Contributor

xJoeWoo commented Dec 26, 2019

If column doesn't have default value and value of column is not set to BatchInsertStatement, the exception BatchDataInconsistentException should be thrown from org.jetbrains.exposed.sql.statements.BatchInsertStatement#validateLastBatch if requiredInTargets is not empty.

However now the exception always says java.util.NoSuchElementException: List is empty.

List is empty.
java.util.NoSuchElementException: List is empty.
	at kotlin.collections.CollectionsKt___CollectionsKt.last(_Collections.kt:362)
	at org.jetbrains.exposed.sql.statements.BatchInsertStatement.allColumnsInDataSet(BatchInsertStatement.kt:59)
	at org.jetbrains.exposed.sql.statements.BatchInsertStatement.getArguments(BatchInsertStatement.kt:63)
	at org.jetbrains.exposed.sql.statements.InsertStatement.arguments(InsertStatement.kt:147)
	at org.jetbrains.exposed.sql.QueriesKt.batchInsert(Queries.kt:128)

I think fun BatchInsertStatement.handleBatchException in org.jetbrains.exposed.sql.batchInsert is hiding the real error.

Test case:

data class Entity(
    val exist: String = "anything"
)

object TestTable : Table() {
    val id = integer("i").autoIncrement().primaryKey()
    val exist = varchar("e", 9)
    val notExist = varchar("n", 9)
}

fun test() {
    transaction {
        SchemaUtils.create(TestTable)
        TestTable.apply {

            // This will pass
            batchInsert(listOf(Entity())) { this[exist] = it.exist; this[notExist] = "whatever" }.map { println(it[id]) }

            // This throws irrelevant exception
            batchInsert(listOf(Entity())) { this[exist] = it.exist }.map { println(it[id]) }
        }
    }
}

@Tapac
Copy link
Contributor

Tapac commented Dec 26, 2019

Why you're trying to insert into TestTable row without notExist column which non-null and doesn't have a default value?

@xJoeWoo
Copy link
Contributor Author

xJoeWoo commented Dec 26, 2019

My client app got "List is empty" exception from an API. I thought that should be a business logic error because list operations were commonly used in project. However after I traced all codes, I found it was from Exposed library, due to a wrong table was accidentally used.

It would be easier to debug if original BatchDataInconsistentException is thrown with message.

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

No branches or pull requests

2 participants