Reputation: 83
Does SQLAlchemy allow us to define the Table (or Query) such that a specific predicate is automatically appended to all queries on that table? The queries can even come via relationships/associations. I am trying to avoid adding a filter() to every query expression in every module.
Example: table T1 has column col1 and I want every sqla query on that table to automatically add an extra predicate col1='Y' before submitting sql to db.
I want the resulting queries involving t1 object to be like these:
select .. from t1 where ... and t1.col1='Y'
select .. from t2 join t1 on t2.x=t1.x where ... and t1.col1='Y'
This was easily doable in hibernate by adding WHERE="col1='Y'" attribute in xml mapping of table or relationship. (This how it was done in hibernate at least 5 years ago).
I am using declarative base with autoload=True in SQLA version 0.9
Upvotes: 1
Views: 864
Reputation: 75127
there are two recipes for how to achieve this behavior:
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/PreFilteredQuery
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/GlobalFilter
Upvotes: 1