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

NullInt32 - could not determine data type of parameter $1 #281

Closed
Saticmotion opened this issue Jun 8, 2017 · 2 comments
Closed

NullInt32 - could not determine data type of parameter $1 #281

Saticmotion opened this issue Jun 8, 2017 · 2 comments

Comments

@Saticmotion
Copy link

Saticmotion commented Jun 8, 2017

I'm trying to run a query with an optional WHERE:

//userid is pgx.NullInt32
//count is int
//offset is int

//if userid.Valid is false this should evaluate to NULL is NULL, and thus not filter on userid 
rows, err := dbPool.Query(`SELECT id, userid, timestamp, downloadsize FROM videos
						WHERE ($1 IS NULL OR userid=$1) 
						LIMIT $2
						OFFSET $3`, userid, count, offset)

Table videos looks like:

id				uuid
userid			integer
timestamp		timestamp without timezone
downloadsize	integer

But this returns the error could not determine data type of parameter $1 (SQLSTATE 42P08). The error occurs both when userid.Valid is true and when false.

What am I doing wrong?

@jackc
Copy link
Owner

jackc commented Jun 8, 2017

The problem is $1 IS NULL. pgx will use prepared statements under the hood. When preparing that statement PostgreSQL cannot infer what type $1 is. You can see the same thing in the psql client.

jack=# prepare ps as select 'foo' where $1 is null;
ERROR:  could not determine data type of parameter $1

Solution is to include a typecast.

jack=# prepare ps as select 'foo' where $1::int is null;
PREPARE

@Saticmotion
Copy link
Author

Oohh I see! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants