In pure postgres we can write:
SELECT * FROM my_table WHERE 10000 = ANY (array_field);
or
SELECT * FROM my_table WHERE 10000 = ALL (array_field);
How to do the same with the help of sqlalchemy without raw sql?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
a = ANY(b_array)is equivalent toaIN(elements_of_b_array)1.Therefore you can use the
in_()method.I can’t remember ever having used
a = ALL(b_array)in all my years with PostgreSQL. Have you?If you are dealing with an array column and want to test whether it contains a given element (or all elements of a given array) in that column, then you can utilize PostgreSQL array operators
@>(contains) or more appropriately the inverse sibling<@(is contained by).Array operators carry the advantage that they can be supported with a GIN index on the array column (unlike the
ANYconstruct).Your SQL statement:
is (almost)1 equivalent to
I am no expert with SQLAlchemy, but according to the tutorial in the SQLAlchemy manual, you can use any operator:
Bold emphasis mine. Your statement could look like this in SQLA:
Or with alternative input syntax for PostgreSQL array values:
1 There is a subtle difference with NULL handling:
always yields
FALSE.always yield
NULL.If you are not going to query for
NULLvalues, you can ignore this.