python - SQLAlchemy Bulk Select to find existing records -


i'm playing sports data , have dictionary of matches keys unique identifiers of match (date, home team id, away team id) (all cols of matches table). want query matches table in bulk , find keys dict in database. want this, keys in dict.

[(date, home_team, away_team), (date, home_team, away_team), ... n] = matches.keys()  *** 1 match session.query(match).filter_by(date=date, home_team=home_team, away_team=away_team) 

edit* so, suggested tried following query:

ins_matches = session.query(match).filter(tuple_(match.date, match.home_team, match.away_team                     ).in_(unq_keys)).all() 

unq_keys list of tuples (date, home_team_id, away_team_id)

programmingerror: (psycopg2.programmingerror) operator not exist: boolean = integer line 3: ...es.home_team_id, teams.id = matches.away_team_id) in (('2014...                                                              ^ hint:  no operator matches given name , argument type(s). might need add explicit type casts.  [sql: 'select matches.id matches_id, matches.date matches_date, matches.time matches_time, matches.league_id matches_league_id, matches.type matches_type, matches.home_team_id matches_home_team_id, matches.away_team_id matches_away_team_id, matches.stadium_id matches_stadium_id, matches.attendance matches_attendance, matches.duration matches_duration, matches.home_team_odds matches_home_team_odds, matches.away_team_odds matches_away_team_odds, matches.result 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}] 

you can use tuple_ expression combined in_:

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

note depends on rdbms support composite in constructs.


Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -