pgx
pgx copied to clipboard
Passing slice of int64 to a query fails with 'cannot find encode plan'
Description
when i pass in a slice of []int64 to a query with IN clause i.e. select id from t where id in ($1);
, it returns the following error:
failed to encode args[0]: unable to encode []int64{1, 2, 3} into binary format for int8 (OID 20): cannot find encode plan
To Reproduce
package main
import (
"context"
"log"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/jackc/pgx/v5"
)
func TestInSliceQuery(t *testing.T) {
conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
defer closeConn(t, conn)
rows, err := conn.Query(context.TODO(), "create temporary table t2 (id bigserial);")
assert.NoError(t, err)
rows.Close()
args := []int64{1, 2, 3}
rows, err = conn.Query(context.TODO(), "select id from t2 where id in ($1);", args)
assert.NoError(t, err)
rows.Close()
}
Expected behavior I'm pretty sure this used to work before but not sure what happened suddenly. I checked dependencies and runtime and nothing seems to have changed.
Version
- Go:
go1.21.3 darwin/amd64
- PostgreSQL:
PostgreSQL 16.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924, 64-bit
- pgx:
v5.4.3
See https://github.com/jackc/pgx/issues/1630.
I can confirm this, using []int32
slice type as argument for IN
operator in WHERE
caluse throws error below:
failed to encode args[0]: unable to encode []int32{} into binary format for int8 (OID 20): cannot find encode plan
@reza-ebrahimi See https://github.com/jackc/pgx/issues/1630#issuecomment-1576823340
Thank you, sir!
problem caused by things like this where f.id in (@facility_ids) (problematic case ) where f.id = any (@facility_ids) (working version ) ANY is PostgreSQL-specific.