I have the following SQLAlchemy subquery with a join:
ChildModel
id first_name last_name
ParentModel
id first_name last_name
ClassroomModel
id child_id
subquery = PostgresqlSession().\
query(ChildModel, ParentModel).\
outerjoin(ParentModel, ChildModel.last_name == ParentModel.last_name).\
subquery()
query = PostgresqlSession().\
query(subquery, ClassroomModel).\
join(subquery, subquery.c.student_id == ClassroomModel.student_id)
But I'm getting a AmbiguousColumn
error because the subquery has an id
column for both ChildModel and ParentModel
What I'd like to do is do a "SELECT AS" for the subquery. I'm looking at the SQLAlchemy documentation about how to do this with select(), so I tried something like:
subquery = PostgresqlSession().\
query(ChildModel, ParentModel).\
select(ChildModel.c.id.label('student_id'), ParentModel.c.id.label('parent_id')).\
outerjoin(ParentModel, ChildModel.last_name == ParentModel.last_name).\
subquery()
but select() is not available on the query model. How can I do a SELECT AS on a session query in SQLAlchemy? Thanks!