将SqlAlchemy联接查询结果获取为数组(或类似的sthg)

将SqlAlchemy联接查询结果获取为数组(或类似的sthg),sqlalchemy,Sqlalchemy,在我的查询中使用许多与左外部联接相关的表, 我想知道是否有任何方法可以轻松地将查询结果作为一个基本数组,例如phpmyadmin中的类型(我不是指布局) 给定3个表,所有表都被映射,我目前只得到第一个表作为对象的结果,如果表2有任何结果,我必须从中逐行测试,以此类推表3: list_res_table1 = DBSession.query(table1).outerjoin(table2).outerjoin(table3).all() for res_table1 in list_res_

在我的查询中使用许多与左外部联接相关的表, 我想知道是否有任何方法可以轻松地将查询结果作为一个基本数组,例如phpmyadmin中的类型(我不是指布局)

给定3个表,所有表都被映射,我目前只得到第一个表作为对象的结果,如果表2有任何结果,我必须从中逐行测试,以此类推表3:

list_res_table1 = DBSession.query(table1).outerjoin(table2).outerjoin(table3).all()  
for res_table1 in list_res_table1:  
    if res_table1.relationship_to_table2:
        list_res_table2 = res_table1.relationship_to_table2
        for res_table2 in list_res_table2:  
            if res_table2.relationship_to_table3:
                etc.
最好能得到一个可直接访问的对象列表,如:

((table1, table2, None)    #=> no result for table3  
(table1, None, None)      #=> no result for table2  
(table1, table2, table3))  #=> results for all tables
您可以(当然应该)直接这样查询:

list_res_table1 = DBSession.query(table1, table2, table3).outerjoin(table2).outerjoin(table3).all()
连接将首先查看最左边的表。如果需要更多的特殊性,可以添加select_from()和explicit ON子句:

list_res_table1 = DBSession.query(table1, table2, table3).\
                      select_from(table1).\
                      outerjoin(table2, table2.c.id==table1.c.t2id).\
                      outerjoin(table3, table2.c.t3id==table3.c.id).all()