Skip to content Skip to sidebar Skip to footer

Using Postgressql Interval In Sqlalchemy Where The Duration Is Dynamically Stored In Db And Is Not A Parameter

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

Solution 1:

The solution fell out of the SQL when I wrote it manually:

from sqlalchemy import func
from sqlalchemy.dialects.postgresql import INTERVALfrom sqlalchemy.sql.functions import concat
...
company_uuid ='some_uuid'
query = db.session.query(CompanyFlagEntity)\
    .join(CompanyFlagTypeEntity)\  # implicit joinusing fk
    .filter(CompanyFlagEntity.company_uuid == company_uuid)\
    .filter((func.now() - func.cast(concat(db_base_app.CompanyFlagTypeEntity.default_lookback_days, ' DAYS'), INTERVAL)) <= cls.flag_date)

This referenced helped to find out how to create the interval dynamically in the first place.

Post a Comment for "Using Postgressql Interval In Sqlalchemy Where The Duration Is Dynamically Stored In Db And Is Not A Parameter"