Hey all, I have what I feel is a pretty simple tas...
# javascript
d
Hey all, I have what I feel is a pretty simple task but I'm struggling to make sense of the PostgREST docs. Essentially, I have a column` 'voters'` within a
'projects'
table, which is structured as an array` ["tom", "mike", ...]` and I'm trying to filter only rows which contain
"mike"
(for example). How would this be achieved using the REST API?
s
In Postgres, I generally use
&&
to search for a value (or array of values) against a column which contains an array of values - that's the overlap operator. So, in PostgREST, this example seems relevant:
?period=ov.[2017-01-01,2017-06-30]
(from https://postgrest.org/en/stable/api.html#operators) So, applying that to your use case, something like this:
?voters=ov.["tom", "mike"]
That should return any rows which have
tom
or
mike
in the
voters
column
d
@Scott P Thank you so much for jumping in so quickly to help me out. That all makes sense to me. I've tried your advice and am getting this response back. Do you know much about this?
Copy code
projects?voters=ov.["tom", "mike"]
Copy code
{
  "message": "malformed array literal: \"[\"tom\", \"mike\"]\"",
  "code": "22P02",
  "details": "\"[\" must introduce explicitly-specified array dimensions.",
  "hint": null
}
Alright cool, thanks to your help. Looks like curly braces are the trick for my flat array.
?voters=cs.{mike, tom}
or
?voters=ov.{mike, tom}
s
Ah, yeah, my bad, that's something I should have remembered - arrays in postgrest are handled as
{}
instead of
[]