SQLAlchemy Joining with subquery issue

前端 未结 1 764
醉梦人生
醉梦人生 2020-12-10 03:53

I am trying to translate SQL into SQLAlchemy. The SQL version of the query I want is as follows:

SELECT * from calendarEventAttendee
JOIN calendarEventAttend         


        
相关标签:
1条回答
  • 2020-12-10 03:57

    Once you call subquery(), there is no access to objects, but only to columns via .c.{column_name} accessor.

    Do the following for sub_query instead: load only the columns you need in order to avoid any name collisions:

    sub_query = db.session.query(
            Bill.id, Bill.personId, BillToEvent.eventId
        ).join(BillToEvent, BillToEvent.billId == Bill.id).subquery()
    

    Then in your query use column names with .c.column_name:

    query = query.outerjoin(
        sub_query, and_(
            sub_query.c.personId == CalendarEventAttendee.personId, 
            sub_query.c.eventId == CalendarEventAttendee.eventId)
        )
    results = query.all()
    
    0 讨论(0)
提交回复
热议问题