简体   繁体   English

SQL Alchemy从同一个表中加入多个列

[英]SQL Alchemy Join Multiple Columns from same table

class Match(Base):
    __tablename__ = 'matches'

    id = Column(Integer, primary_key=True)
    date = Column(Date, nullable=False)
    time = Column(Time, nullable=True)
    league_id = Column(ForeignKey('leagues.id'), nullable=False, index=True)
    league = relationship('League', backref='matches')
    type = Column(enums.matches_types)
    home_team_id = Column(ForeignKey('teams.id'), nullable=False, index=True)
    home_team = relationship('Team', foreign_keys=[home_team_id], backref='home_matches')
    away_team_id = Column(ForeignKey('teams.id'), nullable=False, index=True)


class Team(Base):
    __tablename__ = 'teams'

    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)
    country_id = Column(ForeignKey('countries.id'), nullable=False, index=True)
    country = relationship('Country', backref='teams')

I need to write a query that joins columns and teams tables displaying information of the teams information for both local and away team. 我需要编写一个连接列和团队表的查询,显示本地和远程团队的团队信息。

Session.query(Match.date, Match.home_team.name, Match_away_team.name).joins(Team)

This returns Can't determine join between 'matches' and 'teams'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly 返回Can't determine join between 'matches' and 'teams'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly Can't determine join between 'matches' and 'teams'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly

First, the reason your code doesn't work is because SQLAlchemy doesn't know whether you want to join to Team via home_team or away_team , so you'll have to tell it. 首先,你的代码不起作用的原因是因为SQLAlchemy不知道你是否想通过home_teamaway_team加入Team ,所以你必须告诉它。 In addition, you'll need to join to Team twice, which further complicates things. 此外,您需要两次加入Team ,这使事情变得更加复杂。

This can be done more easily by using joinedload : 使用joinedload可以更轻松地完成此操作:

matches = session.query(Match).options(joinedload(Match.home_team),
                                       joinedload(Match.away_team))
for m in matches:
    print m.date, m.home_team, m.away_team

m.home_team and m.away_team will be loaded in the same query as m using a JOIN . m.home_teamm.away_team将使用JOIN加载到与m相同的查询中。

If you insist on using an explicit .join() you'll have to alias the Team entities (not tested): 如果您坚持使用显式.join() ,则必须为Team实体(未测试)添加别名

home = aliased(Team)
away = aliased(Team)
q = session.query(Match.date, home, away).join(home, Match.home_team) \
                                         .join(away, Match.away_team)
for date, home_team, away_team in q:
    print date, home_team, away_team

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM