Having a list of 3-tuples :
[(a, b, c), (d, e, f)]
I want to retrieve all the rows from a table where 3 columns matches the tuples. FOr this example, the query WHERE clause could be something like this :
(column_X = a AND column_Y = b AND column_Z = c)
OR (column_X = d AND column_Y = e AND column_Z = f)
How can I create such a request using SQLAlchemy ? In my case the 3-tuples list will contains hundred of elements, and I’m looking for the best scallable solution.
Thanks for your help,
Easiest way would be using SQLAlchemy-provided tuple_ function:
This works with PostgreSQL, but breaks with SQLite. Not sure about other database engines.
Fortunately there’s a workaround that should work on all databases.
Start by mapping out all the items with the
and_expression:And then create an
or_filter that encloses all the conditions:Here’s a simple example:
Which outputs: