I have an SQL query I'm trying to translate to sqlalchemy but it's not the same. Here's the query:
select distinct on (uid) uid, id, amount, type, due_date
from events
where status = 'pending' and due_date <= '2020-09-14'
order by uid, due_date desc;
And here's how I'm trying to write it in sqlalchemy:
today = datetime.today()
query = Events.select().distinct(Events.c.uid).where(
Events.c.status == EventStatus.pending).where(
Events.c.due_date <= today).order_by(
Events.c.uid, Events.c.due_date.desc())
When I print the above query I get:
SELECT DISTINCT events.id, events.uid, events.type, events.status,
events.amount, events.old_tier, events.new_tier, events.due_date,
events.created_at, events.updated_at
FROM events
WHERE events.status = :status_1 AND events.due_date <= :due_date_1
ORDER BY events.uid, events.due_date DESC
Which is not the same thing because my original query is DISTINCT ON
. What is wrong here?
PS: I'm using postgres