How can you select from users based on an array of uuids?

Viewed 48

If I query postgres in heroku, it works fine:

select * from users where users.user_id in ('', '')

but if send the request via postman to index.js it works only if there is one element instead of $1:

select * from users where users.user_id in ($1)

I tried casting:

users.user_id::text 
users.user_id in ($1::text

and

= any( array ([]);
1 Answers

First concatenate your UUIDs into a string with , as a delimiter and then pass it as a parameter to this query:

select * from users where users.user_id = any(string_to_array($1, ',')::uuid[]);

or this one:

select * from users where users.user_id = any(('{'||$1||'}')::uuid[]);

This is a more or less generic approach that can be used in other similar cases too.

Related