This is not a duplicate of this question, because that question is/can be solved using a parameter or constant interval. In my case the interval is defined in a table. My intuition says the following should work:
from sqlalchemy import func
from sqlalchemy.dialects.postgresql import INTERVAL
...
company_uuid = 'some_uuid'
query = db.session.query(CompanyFlagEntity)\
.join(CompanyFlagTypeEntity)\ # implicit join using fk
.filter(CompanyFlagEntity.company_uuid == company_uuid)\
.filter((func.now() - INTERVAL(CompanyFlagTypeEntity.default_lookback_days, 'DAY')) <= CompanyFlagEntity.flag_date)
But I get a the following error:
AttributeError: 'INTERVAL' object has no attribute 'comparator'
The SQL version would be:
select company_flag.*
from company_flag
join company_flag_type on company_flag_type.uuid = company_flag.company_flag_type_uuid
where
company_flag.company_uuid = 'my_uuid' and
(now() - (company_flag_type.default_lookback_days || ' days')::interval) <= flag_date