I want to create a function that takes in a generic select, which assumes a particular column exists in the select, and then add a where condition on this column. Here is the setup:
from sqlalchemy import Table, Column, Integer, String, MetaData
from sqlalchemy import select
metadata = MetaData()
Foo = Table(
'foos', metadata,
Column('id', Integer),
Column('foo', String),
Column('status', String),
)
Bar = Table(
'bars', metadata,
Column('id', Integer),
Column('bar', String),
Column('status', String),
)
The following is along the lines of what I would like, however it results in a natural join:
def add_common_where_conditions(sel):
return sel.where(sel.c.status == 'ACTIVE')
def get_foos():
sel = select([Foo])
sel = add_common_where_conditions(sel)
print(sel)
def get_bars():
sel = select([Bar])
sel = add_common_where_conditions(sel)
print(sel)
The output of get_foos() is:
SELECT foos.id, foos.foo, foos.status
FROM foos, (SELECT foos.id AS id, foos.foo AS foo, foos.status AS status
FROM foos) AS anon_1
WHERE anon_1.status = :status_1
I'm not sure how I can get a reference to the original table. For example I tried this, but it only gets a reference to the subquery:
>>> sel = select([Foo])
>>> sel.c[0].table
<sqlalchemy.sql.selectable.Subquery at 0x1d360b7dba8; anon_1>