SQL type conversion failure when setting values for referenced EntityId columns
See original GitHub issueUsing 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:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kspar , never try it. Will look at that issue in the near future.
@plm, thank you for a report. As a workaround you could replace
reference("owner_id", Users.id)
withreference("owner_id", Users)