-
Notifications
You must be signed in to change notification settings - Fork 799
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
"cannot convert 1 to Text" #798
Comments
I think this is caused by the combination of pgx internally using prepared statements and PostgreSQL not being able to determine the type of the placeholders. The simplest solution is to include type information with your placeholders. e.g. $1::int. Alternatively, you could configure pgx to use the simple protocol instead of prepared statements. |
Adding type info to the placeholders fixed it. Thanks a lot. |
How can one configure pgx to use simple protocols? |
Set https://pkg.go.dev/github.com/jackc/pgx/v4?tab=doc#ConnConfig |
I'm using pgx with database sql, is there a way to pass in this config? seems like this config is meant to be used when using |
See the first few paragraphs of the docs for stdlib: https://pkg.go.dev/github.com/jackc/pgx/v4@v4.8.1/stdlib?tab=doc |
Got it :) thanks! |
How can I configure pgx to use the simple protocol from the connection string to Pgpool? My config string is "host=localhost database=test user=postgres password=test" |
Hello @jackc , On a side note, changing the query to |
There shouldn't be anything special about json operators. Can you provide a executable code sample? |
@jackc here is an example of a non working query, here we're connecting to postgres to a
As mentioned in the code output shown in the gists comment, with or without setting https://gist.github.com/jimenez/48a52c4ddad16636869f7b78adf2e527 |
I was able to narrow it to an even smaller case. package main
import (
"context"
"database/sql"
"fmt"
"os"
"github.com/jackc/pgx/v4"
_ "github.com/jackc/pgx/v4/stdlib"
)
func main() {
doStdlib()
doNative()
}
func doStdlib() {
db, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "stdlib: Unable to connect to database: %v\n", err)
return
}
defer db.Close()
var id string
err = db.QueryRow("SELECT $1::text", 2).Scan(&id)
if err != nil {
fmt.Fprintf(os.Stderr, "stdlib: QueryRow failed: %v\n", err)
return
}
fmt.Println("stdlib:", id)
}
func doNative() {
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Fprintf(os.Stderr, "native: Unable to connect to database: %v\n", err)
return
}
defer conn.Close(context.Background())
var id string
err = conn.QueryRow(context.Background(), "SELECT $1::text", 2).Scan(&id)
if err != nil {
fmt.Fprintf(os.Stderr, "native: QueryRow failed: %v\n", err)
return
}
fmt.Println("native:", id)
}
However, simple protocol works for me.
The problem is the SQL parameter is a string and the argument is an int. pgx does not automatically make that conversion. If you are always using an int in that query you could probably use something like: SELECT * WHERE fields ->> '_version' = ($1::int)::text This pgx behavior could be changed. e.g. case int:
*dst = Text{String: fmt.Sprint(value), Status: Present} But I'm not sure if that's a good idea or not. Where should the line be drawn between convenience and strict typing. I'm pretty sure it is a bad idea to automatically convert everything to text. That could easily end up with someone silently losing data (e.g. by storing a pointer address instead of the contents). So defaulting to Then there's also the question of if this should be symmetrical. If an |
@jackc thanks for your explanation and looking into it. I understand that Pgx doesn't do the automatic type conversion (like Pq) so looks like I need to update my queries or my code, I just have one question regarding: |
The double cast |
Hi,
I am trying to update several rows with a single query. Query works fine when I run it against the db but fails when I use placeholders.
Without typecasting I get this error: ERROR: operator does not exist: bigint = text (SQLSTATE 42883)
When I print the query and the variables, I get these:
Query:
Values slice:
The error I'm getting: cannot convert 1 to Text
I am using pgx with database/sql.
Is this related to the pgx driver?
Thanks
The text was updated successfully, but these errors were encountered: