I feel like this will be answered elsewhere, but for the life of me I can't find the correct search term.
I have a subquery that selects 2 values, id
and MAX(date)
. The date is required for getting the latest value when using GROUP BY
, but it is not needed after this stage.
How can I "discard" the date
column so that I'm able to use the id IN (subquery)
statement? In the meantime I'm selecting func.max(Model.id)
to get around the issue.
Here's a trimmed down example of what I'm attempting to do:
# Get the IDs of each latest link to a relationship
>>> subquery = session.query(Model.id, func.max(Model.date)).group_by(Model.relationship_id)
# Use these IDs as part of another query
>>> query = session.query(Model.id).filter(Model.id.in_(subquery))
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) sub-select returns 2 columns - expected 1