I have a postgres table with 2 columns that I use in a query:
start_date_detail: date
flex: jsonb
Example of the flex field:
{
"communication": [
{
"remind_on": "[email protected]",
"type": "email",
"remind_date": -150
},
{
"remind_on": "+31612345678",
"type": "sms",
"remind_date": -360
}
]
}
I need to select all records that have a remind date that lies in the last week, so (pseudo code):
(now() - 1-week) < (start_date_detail + remind_date) < now()
How can I realise that with sqlalchemy?
Because it's a calculated value in the query, I don't know how to do this. In postgres I cam up with this, and that works:
SELECT * FROM time_item
WHERE
(start_date_detail + INTERVAL '1 second' * (flex->'communication'->0->>'remind_date')::numeric <= NOW())
OR (start_date_detail + INTERVAL '1 second' * (flex->'communication'->1->>'remind_date')::numeric <= NOW())
How to put this in sqlalchemy?
One more thing: In the above query I add every communication item to the where clause. How can I make this more flexible? That is that I don't need to put a where clause for every communication item.