为什么SQLAlchemy查询返回“ NoneType”对象?

时间:2020-09-16 08:40:46

标签: python sqlalchemy

我建立了以下查询:

        matches = dal.session.query(Match)
        games_matches = dal.session.query(
            games_table.ID_T_G.label("ID_T_G"),
            games_table.ID_R_G.label("ID_R_G"),
            games_table.ID1_G.label("ID1_G"),
            games_table.ID2_G.label("ID2_G"),
        ).subquery()
        matches_p1_winner = (
            matches
            .join(
                games_matches,
                and_(
                    Match.tour_id.__eq__(tour_id),
                    Match.tours_ID_T.__eq__(games_matches.c.ID_T_G),
                    Match.rounds_ID_R.__eq__(games_matches.c.ID_R_G),
                    Match.today_players_ID1.__eq__(games_matches.c.ID1_G),
                    Match.today_players_ID2.__eq__(games_matches.c.ID2_G)
                )
            )
        )
        matches_p1_winner = matches_p1_winner.filter(
            or_(
                Match.games_players_ID1.__eq__(None),
                Match.games_players_ID2.__eq__(None),
            )
        )
        for match in matches_p1_winner:
            match.games_players_ID1 = match.today_players_ID1
            match.games_players_ID2 = match.today_players_ID2
        dal.session.commit()

我在match.games_players_ID1 = match.today_players_ID1行的一次迭代中遇到错误:

Exception has occurred: AttributeError
'NoneType' object has no attribute 'today_players_ID1'

我假设这意味着查询记录为None。如果查询结果有效,该怎么办?


编辑:

根据注释中的要求为matches_p1_winner添加了SQL:

SELECT belgarath.match_.id_ AS belgarath_match__id_, belgarath.match_.date_time_scheduled AS belgarath_match__date_time_scheduled, belgarath.match_.date_time_actual AS belgarath_match__date_time_actual, belgarath.match_.tour_id AS belgarath_match__tour_id, belgarath.match_.`tours_ID_T` AS `belgarath_match__tours_ID_T`, belgarath.match_.`rounds_ID_R` AS `belgarath_match__rounds_ID_R`, belgarath.match_.`today_players_ID1` AS `belgarath_match__today_players_ID1`, belgarath.match_.`today_players_ID2` AS `belgarath_match__today_players_ID2`, belgarath.match_.`games_players_ID1` AS `belgarath_match__games_players_ID1`, belgarath.match_.`games_players_ID2` AS `belgarath_match__games_players_ID2`, belgarath.match_.uncertainty_bin AS belgarath_match__uncertainty_bin, belgarath.match_.p1_win_pred_ogion AS belgarath_match__p1_win_pred_ogion, belgarath.match_.p1_win_pred_rf_ogion AS belgarath_match__p1_win_pred_rf_ogion, belgarath.match_.p1_win_pred_ged AS belgarath_match__p1_win_pred_ged, belgarath.match_.p1_win_pred_rf_ged AS belgarath_match__p1_win_pred_rf_ged, belgarath.match_.url AS belgarath_match__url, belgarath.match_.winning_player AS belgarath_match__winning_player, belgarath.match_.completed_sets AS belgarath_match__completed_sets, belgarath.match_.result_type_id AS belgarath_match__result_type_id, belgarath.match_.p1_pinnacle_closing_odds AS belgarath_match__p1_pinnacle_closing_odds, belgarath.match_.p2_pinnacle_closing_odds AS belgarath_match__p2_pinnacle_closing_odds, belgarath.match_.post_match_data_retrieved AS belgarath_match__post_match_data_retrieved 
FROM belgarath.match_ INNER JOIN (SELECT oncourt.games_atp.`ID_T_G` AS `ID_T_G`, oncourt.games_atp.`ID_R_G` AS `ID_R_G`, oncourt.games_atp.`ID1_G` AS `ID1_G`, oncourt.games_atp.`ID2_G` AS `ID2_G` 
FROM oncourt.games_atp) AS anon_1 ON belgarath.match_.tour_id = %(tour_id_1)s AND belgarath.match_.`tours_ID_T` = anon_1.`ID_T_G` AND belgarath.match_.`rounds_ID_R` = anon_1.`ID_R_G` AND belgarath.match_.`today_players_ID1` = anon_1.`ID1_G` AND belgarath.match_.`today_players_ID2` = anon_1.`ID2_G` 
WHERE belgarath.match_.`games_players_ID1` IS NULL OR belgarath.match_.`games_players_ID2` IS NULL

0 个答案:

没有答案