-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
When using "pgx" driver, sqlx interprets Postgres integer[] array as string and fails to Scan() #193
Comments
The default type is |
Closing this as it's been open for a long time without any updates and it looks like it's a discrepancy in the driver. |
I reproduced the problem. Complete code below : package main
import (
"fmt"
"github.com/jackc/pgx"
_ "github.com/jackc/pgx/stdlib"
"github.com/jmoiron/sqlx"
)
type User struct {
Field []int32
}
const SQL = "SELECT field FROM testint32"
func main() {
db := connectPgx()
dropTable(db)
createTable(db)
fmt.Println("TEST PGX ...")
testPgx(db)
fmt.Println("TEST SQLX ...")
testSqlx()
}
func dropTable(db *pgx.Conn) {
_, err := db.Exec("DROP TABLE IF EXISTS testint32")
if err != nil {
panic(err)
}
}
func createTable(db *pgx.Conn) {
_, err := db.Exec("CREATE TABLE testint32( field integer[] NOT NULL DEFAULT ARRAY[]::integer[])")
if err != nil {
panic(err)
}
_, err = db.Exec("INSERT INTO testint32 VALUES(ARRAY[1, 2]::integer[])")
if err != nil {
panic(err)
}
}
func testPgx(db *pgx.Conn) {
var x []int32
db.QueryRow(SQL).Scan(&x)
fmt.Printf("X: %+v\n\n", x)
}
func connectPgx() *pgx.Conn {
conn, err := pgx.Connect(pgx.ConnConfig{
Host: "localhost",
Port: 5432,
Database: "mydb",
User: "postgres",
Password: "",
TLSConfig: nil,
UseFallbackTLS: false,
FallbackTLSConfig: nil,
Logger: nil,
RuntimeParams: map[string]string{
"client_encoding": "UTF8",
"timezone": "UTC",
},
})
if err != nil {
panic(err)
}
return conn
}
func testSqlx() {
db := sqlx.MustConnect("pgx", "postgres://postgres:@localhost:5432/mydb?sslmode=disable")
var u User
err := db.Get(&u, SQL)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", u)
} Output:
|
I would like to ask if there was ever a solution to this problem since I am now running into the exact same error. |
drop sqlx. It is not complete. |
Old issue, but if someone gets here please use array types from https://pkg.go.dev/github.com/lib/pq (if you wanna keep using sqlx)
|
When using the
pgx
driver the following query whereids
is a Postgres arrayinteger[]
fails with the error:sql: Scan error on column index 0: unsupported driver -> Scan pair: string -> *[]int32
If I use the
pgx
driver directly, the array is decoded into a Go slice fine. Here's the code:Any idea why using
sqlx
is interpreting the Postgres arrayinteger[]
as a string?The text was updated successfully, but these errors were encountered: