How to resolve ambigious colum after join in sqlalchemy

python, sql, sqlalchemy

Solution

for a subquery you typically want to name only those columns you need (since the subq isn't used to load full objects) and you then can use label() for anything additional:

subq = sess.query(object.a, object.b, object.hid.label('o_hid'), 
                  robject.c, robject.hid.label('r_hid')).filter(..).subquery()

the subquery then names those columns based on the label name:

   query(Something).join(subq, subq.c.o_hid == Something.q).filter(subq.c.r_hid == 5)

Problem

When I join two tables (objects) using statement as ``` session.query(object, robject).filter(getattr(object.c, "hid")==getattr(robject.c,\ )).subquery() ``` results in column reference "hid" is ambiguous since both tables have hid column. How should I resolve this? Thanks

Original source