-
Notifications
You must be signed in to change notification settings - Fork 922
Wrong type in "Update case when then" query #586
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
Comments
That's because PL/PgSQL knows that the type of DO $$
DECLARE
sql text := 'UPDATE test SET order = (CASE id WHEN $1 THEN $2 WHEN $3 THEN $4 END)';
BEGIN
EXECUTE sql
USING '1', '1', '2', '2'; -- notice the quotes here
END$$; you get the same error. This is not really a problem in the driver. You could try and argue that postgres should be smarter about inferring the types for $2 and $4 in this case, but I would be surprised if you got anywhere with that argument. I don't see any obvious workarounds, so I'm going to suggest adding the casts. |
@johto Thanks for the answer. But, why does this error only appear after "THEN"?
|
The expressions |
Probably the driver could help the database to infer the types here? If I
understand correctly lib/pq doesn't send argument types along with their
values and leaves the database to infer them.
Unfortunately database/sql doesn't allow specifying argument types
explicitly either. See golang/go#12383 and referenced issue.
…On Mar 21, 2017 3:43 PM, "Marko Tiikkaja" ***@***.***> wrote:
The expressions 200 and 300 are of type integer. If use use an unknown
type literal (e.g. '200') you should see the same problem again.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#586 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AGy9A0NyX0L5Xb3kTqwZdWcyGpbpAImeks5rn_AOgaJpZM4Mirrc>
.
|
I can't get excited about that idea. That would mean that queries done through Prepare() would act differently (because we can't know the parameter types at that point) from plain Query() calls. It could also break applications that previously relied on type inference in some cases; function calls are particularly vulnerable here. Besides, if the application already knows it's an integer, it shouldn't be that difficult to tell that to the database in the query. |
Just so people understand how vulnerable, exactly: =# create function foof(a text, b int) returns int as $$ select 1 $$ language sql;
CREATE FUNCTION
=# prepare qwr as select foof($1, $2); -- both types unknown; current behavior
PREPARE
=# prepare qwr2(unknown, bigint) as select foof($1, $2); -- proposed new behavior
ERROR: function foof(unknown, bigint) does not exist
LINE 1: prepare qwr2(unknown, bigint) as select foof($1, $2); This is because unknown types are not resolved in function calls unless the known types match exactly. And we have to assume the type is "bigint", because database/sql doesn't expose more information than that. This would break WAY too many apps. |
Hello, guys!
I have trouble with the query.
Table:
Query:
If I use psql or IDE and try execute the query, I don't have troubles:
But when I using pq lib execute the query, I have this error:
Soo, I don't understand, why I don't have the error in "id" column.
Is it bug in pq or is a normal behavior of PostgreSQL?
I can set type using "CAST", I know..
PS: PostgreSQL 9.6
The text was updated successfully, but these errors were encountered: