SQLAlchemy Bulk选择查找现有记录

时间:2016-02-08 21:01:57

标签: python sqlalchemy bulk

我正在玩一些体育数据,并且有一个比赛字典,其中的键是比赛的唯一标识符(日期,主队ID,客队ID)(比赛表的所有列)。 我想批量查询匹配表,并找到数据库中已存在的dict中的所有键。 我想这样做,但是对于dict中的所有键。

[(date, home_team, away_team), (date, home_team, away_team), ... N] = matches.keys()

*** only one match
Session.query(Match).filter_by(date=date, home_team=home_team, away_team=away_team)

修改* 所以,正如我所建议的那样,我尝试了以下查询:

ins_matches = Session.query(Match).filter(tuple_(Match.date, Match.home_team, Match.away_team
                    ).in_(unq_keys)).all()

unq_keys是一个元组列表,其中包含(date,home_team_id,away_team_id)

ProgrammingError: (psycopg2.ProgrammingError) operator does not exist: boolean = integer
LINE 3: ...es.home_team_id, teams.id = matches.away_team_id) IN (('2014...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
 [SQL: 'SELECT matches.id AS matches_id, matches.date AS matches_date, matches.time AS matches_time, matches.league_id AS matches_league_id, matches.type AS matches_type, matches.home_team_id AS matches_home_team_id, matches.away_team_id AS matches_away_team_id, matches.stadium_id AS matches_stadium_id, matches.attendance AS matches_attendance, matches.duration AS matches_duration, matches.home_team_odds AS matches_home_team_odds, matches.away_team_odds AS matches_away_team_odds, matches.result AS matches_result \nFROM matches, teams \nWHERE (matches.date, teams.id = matches.home_team_id, teams.id = matches.away_team_id) IN ((%(param_1)s, %(param_2)s, %(param_3)s), (%(param_4)s, %(param_5)s, %(param_6)s), (%(param_7)s, %(param_8)s, %(param_9)s), (%(param_10)s, %(param_11)s, %(param_12)s), (%(param_13)s, %(param_14)s, %(param_15)s))'] [parameters: {'param_15': 17, 'param_11': 14, 'param_6': 18, 'param_5': 12, 'param_1': '2014-10-29', 'param_8': 11, 'param_4': '2014-10-28', 'param_13': '2014-10-28', 'param_7': '2014-10-28', 'param_14': 19, 'param_9': 20, 'param_10': '2014-10-29', 'param_2': 16, 'param_3': 15, 'param_12': 13}]

1 个答案:

答案 0 :(得分:2)

您可以将tuple_表达式与in_结合使用:

session.query(Match).filter(tuple_(Match.date, Match.home_team, Match.away_team).in_(list(matches.keys())))

请注意,这取决于RDBMS对复合IN构造的支持。