简体   繁体   English

SQL炼金术奇怪的行为联接来自同一表的多个列

[英]SQL Alchemy Strange behaviour Join Multiple Columns from same table

I'm trying to perform a query that involves joining two columns from the same column. 我正在尝试执行一个查询,该查询涉及将同一列中的两个列联接在一起。

These are my models: 这些是我的模型:

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_id = Column(ForeignKey('teams.id'), nullable=False, index=True)
    home = relationship('Team', foreign_keys=[home_id], backref='home_matches')
    away_id = Column(ForeignKey('teams.id'), nullable=False, index=True)
    away = relationship('Team', foreign_keys=[away_id], backref='away_matches')


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')

This is what I wrote in SQLAlchemy: 这是我在SQLAlchemy中写的:

self.db_match = Session.query(Match).join(home, Match.home).options(contains_eager(Match.home)
                        ).join(away, Match.away).options(contains_eager(Match.away)
                        ).join(Match.b_reference).options(contains_eager(Match.b_reference)
                        ).filter(home.name == self.match['home']['name']
                        ).filter(away.name == self.match['away']['name']
                        ).filter(Match.date == self.match['date'])

The translation that SQLAlchemy does to pure SQL is the following: SQLAlchemy对纯SQL的转换如下:

SELECT teams_1.name, teams_2.name, matches.id, matches_b_reference_codes.code
FROM teams, matches JOIN teams AS teams_1 ON teams_1.id = matches.home_id 
JOIN teams AS teams_2 ON teams_2.id = matches.away_id 
JOIN matches_b_reference_codes ON matches.id = matches_b_reference_codes.match_id 
WHERE teams_1.name = 'Portland Trail Blazers' AND teams_2.name = 'Miami Heat' AND matches.date = '2011-01-09';

But this is what I need: 但这是我需要的:

SELECT teams_1.name, teams_2.name, matches.id, matches_b_reference_codes.code
**FROM matches JOIN teams AS teams_1 ON teams_1.id = matches.home_id** 
JOIN teams AS teams_2 ON teams_2.id = matches.away_id 
JOIN matches_b_reference_codes ON matches.id = matches_b_reference_codes.match_id 
WHERE teams_1.name = 'Portland Trail Blazers' AND teams_2.name = 'Miami Heat' AND matches.date = '2011-01-09';

You are using contains_eager incorrectly. 您使用的contains_eager错误。 You need to specify the alias parameter when you're joining to an alias: 加入alias时,需要指定alias参数:

home = aliased(Team)
away = aliased(Away)
Session.query(Match).join(home, Match.home) \
                    .options(contains_eager(Match.home, alias=home)) \
                    .join(away, Match.away) \
                    .options(contains_eager(Match.away, alias=away)) \
                    ... \
                    .filter(...)

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

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