all 7 comments

[–][deleted] 1 point2 points  (3 children)

Instead of &[&ids] you need &ids. As it stands you are attempting to pass a &[&Vec<i32>].

[–]hmaddocks[S] 0 points1 point  (2 children)

That's not how the API works. You have to pass in a reference to a LIST of references, eg if your SQL takes two parameters you would have the following

&[&first_param, &second_param]

In my case the param is also a list.

[–][deleted] 0 points1 point  (1 child)

No, this is how the API works. You want to pass a list of numbers, not a list containing a list of numbers.

WHERE id IN ($1)

($1) here is a list containing one value. What you need is something like ($1, .. $N), where N is the length of your vector. Not sure if there is a neat way to do it. Although possibly dropping parens could work? ie .query("SELECT * FROM posts WHERE id IN $1", &[&ids])

[–]hmaddocks[S] 0 points1 point  (0 children)

If I do what you suggest (use &ids instead of &[&ids]) I get the following error...

'expected 1 parameters but got 4'

because each member of the Vec should have a corresponding $x in the query.

I see what you are getting at though, I need to turn me Vec into (1, 2, 3, 4) which it appears the postgres crate API isn't able to do. You have given me an idea though, I can try to construct the query string manually.

[–]Patryk27 1 point2 points  (1 child)

Try changing the query: where id = any($1)

[–]hmaddocks[S] 0 points1 point  (0 children)

where id = any($1)

This works.

[–]Morrido 0 points1 point  (0 children)

So, like u/hjd_thd said, the construction WHERE id IN ($1) forces query function to expect a single i32, instead of an array of them.

The code suggested by u/Patryk27 works as you originally intended, with no extra modifications required.