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

asUpdateAndReturnGeneratedKey does not work with postgresql returning id #22

Open
tbohnen opened this issue Oct 18, 2018 · 7 comments
Open

Comments

@tbohnen
Copy link

tbohnen commented Oct 18, 2018

When we execute a postgresql insert statement that does returning id, we get the following exception:
A result was returned when none was expected

The below example will throw the exception: (Untested but created from other code that did)

 val insertQuery = "insert into test(name)  values (?) RETURNING id;"

using(kotliquery.sessionOf(HikariCP.dataSource())){session ->
                val query = queryOf(insertQuery,
                        "test name",
                ).asUpdateAndReturnGeneratedKey
                session.run(query)
        }

Stacktrace:

org.postgresql.util.PSQLException: A result was returned when none was expected.

at org.postgresql.jdbc.PgStatement.getNoResultUpdateCount(PgStatement.java:254)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:134)
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java)
at kotliquery.Session$updateAndReturnGeneratedKey$1.invoke(Session.kt:158)
at kotliquery.Session$updateAndReturnGeneratedKey$1.invoke(Session.kt:17)
at kotliquery.LoanPattern.using(LoanPattern.kt:11)
at kotliquery.PackageKt.using(package.kt:30)
at kotliquery.Session.updateAndReturnGeneratedKey(Session.kt:157)
at kotliquery.action.UpdateAndReturnGeneratedKeyQueryAction.runWithSession(UpdateAndReturnGeneratedKeyQueryAction.kt:9)
at kotliquery.Session.run(Session.kt:175)
@tbohnen tbohnen changed the title asUpdateAndReturnGeneratedKey does not work with postgresql RETURNING id; asUpdateAndReturnGeneratedKey does not work with postgresql returning id Oct 18, 2018
@seratch
Copy link
Owner

seratch commented Oct 18, 2018

A result was returned when none was expected

I've never seen the case no value returned is expected when using RETURN_GENERATED_KEYS for a JDBC statement. What is the definition of your database table? If you really need the support for your case, you may need to work on pull requests. I myself don't have any plans to dedicate my time to work on it.

@seratch
Copy link
Owner

seratch commented Oct 19, 2018

As you may already know, the library supports returning id syntax when the id column is serial type or whatever with auto-generated value (although the CI builds don't have tests using PostgreSQL/MySQL, I've made sure that it works with PostgreSQL/MySQL). That'd be enough as far as I know.

@tbohnen
Copy link
Author

tbohnen commented Oct 19, 2018

Thanks for the response.
ID definition is: id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY

Which is similar to serial, just new syntax (for the most part) in postgresql 10.
So given the above code and the id, is it you're understanding that it should work? If so, I can dig and see why not.

@tbohnen
Copy link
Author

tbohnen commented Oct 21, 2018

Just to add: If I remove the returning id; it throws an exception:

caused by: org.postgresql.util.PSQLException: ResultSet not positioned properly, perhaps you need to call next.
	at org.postgresql.jdbc.PgResultSet.checkResultSet(PgResultSet.java:2768)
	at org.postgresql.jdbc.PgResultSet.getLong(PgResultSet.java:2072)
	at com.zaxxer.hikari.pool.HikariProxyResultSet.getLong(HikariProxyResultSet.java)
	at kotliquery.Session$updateAndReturnGeneratedKey$1.invoke(Session.kt:161)
	at kotliquery.Session$updateAndReturnGeneratedKey$1.invoke(Session.kt:17)
	at kotliquery.LoanPattern.using(LoanPattern.kt:11)
	at kotliquery.PackageKt.using(package.kt:30)
	at kotliquery.Session.updateAndReturnGeneratedKey(Session.kt:157)
	at kotliquery.action.UpdateAndReturnGeneratedKeyQueryAction.runWithSession(UpdateAndReturnGeneratedKeyQueryAction.kt:9)
	at kotliquery.Session.run(Session.kt:175)

@seratch
Copy link
Owner

seratch commented Oct 22, 2018

@tbohnen I've never checked if the library works with the column definition yet.

@nieldw
Copy link

nieldw commented Apr 18, 2019

@tbohnen @seratch A workaround is to use asSingle or just single.

val insertQuery = "insert into test(name)  values (?) RETURNING id;"

using(kotliquery.sessionOf(HikariCP.dataSource())) { session ->
    val query = queryOf(insertQuery, "test name")
    session.single(query) { row -> row.int("id") }
}

@Saxintosh
Copy link

Saxintosh commented Dec 5, 2019

Release: 1.3.0 with Postgres

I got this problem but then resolved opening the session setting returnGeneratedKey = true

	using(sessionOf(datasource, true)) { session ->
		...
	}

... no needs to use INSERT .... RETURNING ID

hope it can be useful!

chsko added a commit to navikt/helse-spesialist that referenced this issue Oct 21, 2024
Må dele opp spørringen fordi kotliquery ikke støtter å
benytte både `returnGeneratedKeys=true` og samtidig spesifisere
returtype fra en insert samtidig. Se bug: seratch/kotliquery#22
chsko added a commit to navikt/helse-spesialist that referenced this issue Oct 21, 2024
Må dele opp spørringen fordi kotliquery ikke støtter å
benytte både `returnGeneratedKeys=true` og samtidig spesifisere
returtype fra en insert samtidig. Se bug: seratch/kotliquery#22
chsko added a commit to navikt/helse-spesialist that referenced this issue Oct 21, 2024
Må dele opp spørringen fordi kotliquery ikke støtter å
benytte både `returnGeneratedKeys=true` og samtidig spesifisere
returtype fra en insert samtidig. Se bug: seratch/kotliquery#22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants