3 ms·
+1 on SQLAlchemy. I find myself using the SQL expression API all the time these days for ad hoc queries where in the past I would have fired up psql. from
by chucksmash 10y ago
+1 on SQLAlchemy. I find myself using the SQL expression API all the time these days for ad hoc queries where in the past I would have fired up psql.
from sqlalchemy import create_engine, func, select
query = select([
my_table.c.column1,
my_table.c.column2
]).select_from(
my_table
).where(
my_table.c.column3 == datetime.date(2016, 5, 10)
).where(
func.coalesce(func.array_length(my_table.c.my_stupid_column4_is_an_array, 1), 0) > 0
)
The above:
a) isn't too much more boilerplate-y than the actual SQL statement itself but is composable so later on in my IPython session I can easily do
other_query = query.order_by(-my_table.c.column1).limit(10)
b) can always just be written in straight SQL if I don't remember the specific SQLAlchemy invocation or can even be written as a mish-mash of SQLAlchemy expression language and textual SQL.