question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQL type conversion failure when setting values for referenced EntityId columns

See original GitHub issue

Using Exposed 0.17.3, Kotlin 1.3.50, and PostgresQL JDBC 42.2.6, I am unable to create new IdTable-derived entity rows which reference other IdTable-derived entity IDs.

Given a simple schema:

object Users : IntIdTable() {
    val name = varchar("name", 10)
}

object Messages : IntIdTable() {
    val owner = reference("owner_id", Users.id)
    val content = text("content")
}

And given a simple insert of a new Messages row:

transaction {
    val id = Users.insertAndGetId {
        it[name] = "Example"
    }
    Messages.insert {
        it[owner] = id // type is EntityId<Int>
        it[content] = "Hello world!"
    }
}

An exception is thrown when the user ID is bound to the prepared statement for the Messages insert:

org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.jetbrains.exposed.dao.EntityID. Use setObject() with an explicit Types value to specify the type to use.
	at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:955)
	at org.jetbrains.exposed.sql.IColumnType$DefaultImpls.setParameter(ColumnType.kt:56)
	at org.jetbrains.exposed.sql.ColumnType.setParameter(ColumnType.kt:60)
	at org.jetbrains.exposed.sql.statements.StatementKt.fillParameters(Statement.kt:116)
	at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed(Statement.kt:51)
...

Stepping through the code in a debugger, the column type is INTEGER but the object type is still EntityId rather than Int (the Int value has not been unwrapped).

Is this the expected behavior? Is the example above representative of the expected code for this use case?

A sample project with DAO and DSL test cases is available at plm/exposed-entityid-example.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Tapaccommented, Jan 3, 2020

@kspar , never try it. Will look at that issue in the near future.

1reaction
Tapaccommented, Sep 11, 2019

@plm, thank you for a report. As a workaround you could replace reference("owner_id", Users.id) with reference("owner_id", Users)

Read more comments on GitHub >

github_iconTop Results From Across the Web

sql - type conversion failure - Stack Overflow
The reason you are getting an error is that you cannot use a string function on a null value. First you need to...
Read more >
How to Get SQL Server Data-Conversion Horribly Wrong
Conversion failed when converting the varchar value 'does not apply' to data type int. The database engine is trying to convert the value...
Read more >
How to fix failed data type conversion error - Microsoft Q&A
Conversion failed when converting the varchar value 'Product_Type_ID' to data type int. I have double checked and the Product_Type_ID column in ...
Read more >
Parameters - Snowflake Documentation
Specifies whether to set the schema for unloaded Parquet files based on the logical column data types (i.e. the types in the unload...
Read more >
INSERT INTO SELECT statement overview and examples
In SQL, we use the SQL INSERT INTO statement to insert records. ... Conversion failed when converting the varchar value 'raj' to data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found