如何使用sqlalchemy加入和联合?

时间:2017-04-03 07:13:29

标签: python sqlalchemy

query1 = self.session.query(ID_Streets, ID_Nps, ID_Streets_history, Modify_reason)\
            .join(ID_Nps, ID_Nps.id_np == ID_Streets.id_np)\
            .outerjoin(ID_Streets_history, text('(ID_Streets.id_np=ID_Streets_history.id_np '\
                'AND ID_Streets.id_street=ID_Streets_history.id_street)'))\
            .outerjoin(Modify_reason, text('(ID_Streets_history.code_reason=Modify_reason.code_reason '\
                'AND ID_Streets_history.code_detail=Modify_reason.code_detail)'))\
            .group_by(ID_Streets.id_np, ID_Streets.id_street, ID_Nps.id_region,\
                ID_Nps.id_atu, ID_Nps.id_selsov, ID_Nps.id_np, ID_Streets_history.id_np,\
                ID_Streets_history.id_street, ID_Streets_history.id_row,\
                Modify_reason.code_reason, Modify_reason.code_detail)

query2 = self.session.query(\
                sql.null().label('id_streets_id_np'),\
                sql.null().label('id_streets_id_street'),\
                sql.null().label('id_streets_name_street'),\
                sql.null().label('id_streets_type_street'),\
                ID_Streets_history, ID_Nps, Modify_reason)\
                .outerjoin(ID_Nps, ID_Nps.id_np == ID_Streets_history.id_np)\
                .outerjoin(Modify_reason, text('(ID_Streets_history.code_reason=Modify_reason.code_reason '\
                    'AND ID_Streets_history.code_detail=Modify_reason.code_detail)'))\
                .group_by(ID_Nps.id_region,\
                    ID_Nps.id_atu, ID_Nps.id_selsov, ID_Nps.id_np, ID_Streets_history.id_np,\
                    ID_Streets_history.id_street, ID_Streets_history.id_row,\
                    Modify_reason.code_reason, Modify_reason.code_detail)

query = query1.union(query2)

错误:

  

不知道如何从NULL加入;请使用select_from()建立此连接的左实体/可选择

如果修复query2:

self.session.query(\
                ID_Streets_history, ID_Nps, Modify_reason\
                sql.null().label('id_streets_id_np'),\
                sql.null().label('id_streets_id_street'),\
                sql.null().label('id_streets_name_street'),\
                sql.null().label('id_streets_type_street'))

错误:

  

(Psycopg2.ProgrammingError)错误:在UNION构造中,你不能推广变量和整数字符的类型   第4行:... istory.id_street AS id_streets_history_id_street,id_streets ..

生成的SQL:

SELECT anon_1.id_streets_id_np AS anon_1_id_streets_id_np, anon_1.id_streets_id_street AS anon_1_id_streets_id_street, anon_1.id_streets_name_street AS anon_1_id_streets_name_street, anon_1.id_streets_type_street AS anon_1_id_streets_type_street, anon_1.id_nps_id_region AS anon_1_id_nps_id_region, anon_1.id_nps_id_atu AS anon_1_id_nps_id_atu, anon_1.id_nps_id_selsov AS anon_1_id_nps_id_selsov, anon_1.id_nps_id_np AS anon_1_id_nps_id_np, anon_1.id_nps_name_np AS anon_1_id_nps_name_np, anon_1.id_nps_type_np AS anon_1_id_nps_type_np, anon_1.id_nps_okato AS anon_1_id_nps_okato, anon_1.id_nps_oktmo AS anon_1_id_nps_oktmo, anon_1.id_streets_history_id_np AS anon_1_id_streets_history_id_np, anon_1.id_streets_history_id_street AS anon_1_id_streets_history_id_street, anon_1.id_streets_history_id_row AS anon_1_id_streets_history_id_row, anon_1.id_streets_history_name_street AS anon_1_id_streets_history_name_street, anon_1.id_streets_history_type_street AS anon_1_id_streets_history_type_street, anon_1.id_streets_history_code_reason AS anon_1_id_streets_history_code_reason, anon_1.id_streets_history_code_detail AS anon_1_id_streets_history_code_detail, anon_1.id_streets_history_creater AS anon_1_id_streets_history_creater, anon_1.id_streets_history_createddate AS anon_1_id_streets_history_createddate, anon_1.id_streets_history_updater AS anon_1_id_streets_history_updater, anon_1.id_streets_history_updateddate AS anon_1_id_streets_history_updateddate, anon_1.id_streets_history_recordcomment AS anon_1_id_streets_history_recordcomment, anon_1.id_streets_history_sourcecomment AS anon_1_id_streets_history_sourcecomment, anon_1.modify_reason_code_reason AS anon_1_modify_reason_code_reason, anon_1.modify_reason_code_detail AS anon_1_modify_reason_code_detail, anon_1.modify_reason_comment_reason AS anon_1_modify_reason_comment_reason 
FROM (SELECT id_streets.id_np AS id_streets_id_np, id_streets.id_street AS id_streets_id_street, id_streets.name_street AS id_streets_name_street, id_streets.type_street AS id_streets_type_street, id_nps.id_region AS id_nps_id_region, id_nps.id_atu AS id_nps_id_atu, id_nps.id_selsov AS id_nps_id_selsov, id_nps.id_np AS id_nps_id_np, id_nps.name_np AS id_nps_name_np, id_nps.type_np AS id_nps_type_np, id_nps.okato AS id_nps_okato, id_nps.oktmo AS id_nps_oktmo, id_streets_history.id_np AS id_streets_history_id_np, id_streets_history.id_street AS id_streets_history_id_street, id_streets_history.id_row AS id_streets_history_id_row, id_streets_history.name_street AS id_streets_history_name_street, id_streets_history.type_street AS id_streets_history_type_street, id_streets_history.code_reason AS id_streets_history_code_reason, id_streets_history.code_detail AS id_streets_history_code_detail, id_streets_history.creater AS id_streets_history_creater, id_streets_history.createddate AS id_streets_history_createddate, id_streets_history.updater AS id_streets_history_updater, id_streets_history.updateddate AS id_streets_history_updateddate, id_streets_history.recordcomment AS id_streets_history_recordcomment, id_streets_history.sourcecomment AS id_streets_history_sourcecomment, modify_reason.code_reason AS modify_reason_code_reason, modify_reason.code_detail AS modify_reason_code_detail, modify_reason.comment_reason AS modify_reason_comment_reason 
FROM id_streets JOIN id_nps ON id_nps.id_np = id_streets.id_np LEFT OUTER JOIN id_streets_history ON (ID_Streets.id_np=ID_Streets_history.id_np AND ID_Streets.id_street=ID_Streets_history.id_street) LEFT OUTER JOIN modify_reason ON (ID_Streets_history.code_reason=Modify_reason.code_reason AND ID_Streets_history.code_detail=Modify_reason.code_detail) GROUP BY id_streets.id_np, id_streets.id_street, id_nps.id_region, id_nps.id_atu, id_nps.id_selsov, id_nps.id_np, id_streets_history.id_np, id_streets_history.id_street, id_streets_history.id_row, modify_reason.code_reason, modify_reason.code_detail UNION SELECT id_streets_history.id_np AS id_streets_history_id_np, id_streets_history.id_street AS id_streets_history_id_street, id_streets_history.id_row AS id_streets_history_id_row, id_streets_history.name_street AS id_streets_history_name_street, id_streets_history.type_street AS id_streets_history_type_street, id_streets_history.code_reason AS id_streets_history_code_reason, id_streets_history.code_detail AS id_streets_history_code_detail, id_streets_history.creater AS id_streets_history_creater, id_streets_history.createddate AS id_streets_history_createddate, id_streets_history.updater AS id_streets_history_updater, id_streets_history.updateddate AS id_streets_history_updateddate, id_streets_history.recordcomment AS id_streets_history_recordcomment, id_streets_history.sourcecomment AS id_streets_history_sourcecomment, id_nps.id_region AS id_nps_id_region, id_nps.id_atu AS id_nps_id_atu, id_nps.id_selsov AS id_nps_id_selsov, id_nps.id_np AS id_nps_id_np, id_nps.name_np AS id_nps_name_np, id_nps.type_np AS id_nps_type_np, id_nps.okato AS id_nps_okato, id_nps.oktmo AS id_nps_oktmo, modify_reason.code_reason AS modify_reason_code_reason, modify_reason.code_detail AS modify_reason_code_detail, modify_reason.comment_reason AS modify_reason_comment_reason, NULL AS id_streets_id_np, NULL AS id_streets_id_street, NULL AS id_streets_name_street, NULL AS id_streets_type_street 
FROM id_streets_history LEFT OUTER JOIN id_nps ON id_nps.id_np = id_streets_history.id_np LEFT OUTER JOIN modify_reason ON (ID_Streets_history.code_reason=Modify_reason.code_reason AND ID_Streets_history.code_detail=Modify_reason.code_detail) GROUP BY id_nps.id_region, id_nps.id_atu, id_nps.id_selsov, id_nps.id_np, id_streets_history.id_np, id_streets_history.id_street, id_streets_history.id_row, modify_reason.code_reason, modify_reason.code_detail) AS anon_1

1 个答案:

答案 0 :(得分:0)

query2 = self.session.query(\
                sql.null().label('id_streets_id_np'),\
                sql.null().label('id_streets_id_street'),\
                sql.null().label('id_streets_name_street'),\
                sql.null().label('id_streets_type_street'),
                ID_Streets_history, ID_Nps, Modify_reason)\
                .select_from(ID_Streets_history, ID_Nps, Modify_reason)\
                .outerjoin(ID_Nps, ID_Nps.id_np == ID_Streets_history.id_np)\
                .outerjoin(Modify_reason, text(\
                    '(ID_Streets_history.code_reason=Modify_reason.code_reason '\
                    'AND ID_Streets_history.code_detail=Modify_reason.code_detail)'))