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

Support for checking if a value is in an array #269

Closed
insanitybit opened this issue Mar 13, 2022 · 0 comments · Fixed by #283
Closed

Support for checking if a value is in an array #269

insanitybit opened this issue Mar 13, 2022 · 0 comments · Fixed by #283
Labels
good first issue Good for newcomers

Comments

@insanitybit
Copy link
Contributor

Summary

CREATE TABLE example (
    values text[]
)

If I want to check if a value is in values, this is how i would write the SQL "raw".

SELECT values 
FROM example 
WHERE "boop" = any(values);

I looked around but didn't see a native way to express this. I ended up doing this instead:

Query:select()
.cond_where(
    Expr::cust_with_values("? = any(populated_fields)", [prop_name.clone()])
)

Native support for checking if a value is in an array would be nice.

The .contains method generates @>, which will fail if a single value is provided since it requires both operands to be arrays.

I also found that .is_in([my_value]), which produces IN ($1) also fails.

In both cases it'll give you the error "operator does not exist: text[] = text".

ANY is described in the postgresql docs here: https://www.postgresql.org/docs/current/functions-comparisons.html#id-1.5.8.30.16

Motivation

Checking if a value is in an array seems like a reasonable thing to do, and in general supporting array operations.

Additional Information

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

Successfully merging a pull request may close this issue.

2 participants