3

我有一个正在尝试构建的查询。该查询似乎部分工作,查询的两个单独部分都返回正确数量的元素。但是,组合查询返回一个空结果集,这是不正确的。

注意:我知道查询 1 和 2 不需要 and_,但我想确保 and_ 按我的预期工作。

查询 1

  • SQLAlchemy 查询

    session.query(Lobby).filter(
        and_(
            Lobby.id == spectator_table.c.lobby_id,
            spectator_table.c.player_id == player.steamid
        )
    ).all()
    
  • 生成的 SQL

    SELECT lobby.id AS lobby_id, lobby.name AS lobby_name, lobby.owner_id AS lobby_owner_id
    FROM lobby, spectator
    WHERE lobby.id = spectator.lobby_id AND spectator.player_id = ?
    

查询 2

  • SQLAlchemy 查询

    session.query(Lobby).filter(
        and_(
            Lobby.id == Team.lobby_id,
            LobbyPlayer.team_id == Team.id,
            LobbyPlayer.player_id == player.steamid
        )
    ).all()
    
  • 生成的 SQL

    SELECT lobby.id AS lobby_id, lobby.name AS lobby_name, lobby.owner_id AS lobby_owner_id
    FROM lobby, team, lobby_player
    WHERE lobby.id = team.lobby_id AND lobby_player.team_id = team.id AND lobby_player.player_id = ?
    

组合查询

  • SQLAlchemy 查询

    session.query(Lobby).filter(
        or_(
            and_(
                Lobby.id == Team.lobby_id,
                LobbyPlayer.team_id == Team.id,
                LobbyPlayer.player_id == player.steamid
            ), and_(
                Lobby.id == spectator_table.c.lobby_id,
                spectator_table.c.player_id == player.steamid
            )
        )
    ).all()
    
  • 生成的 SQL

    SELECT lobby.id AS lobby_id, lobby.name AS lobby_name, lobby.owner_id AS lobby_owner_id
    FROM lobby, team, lobby_player, spectator
    WHERE lobby.id = team.lobby_id AND lobby_player.team_id = team.id AND lobby_player.player_id = ? OR lobby.id = spectator.lobby_id AND spectator.player_id = ?
    

模型

spectator_table = Table('spectator', Base.metadata,
    Column('lobby_id', Integer, ForeignKey('lobby.id'), primary_key=True),
    Column('player_id', Integer, ForeignKey('player.steamid'),
        primary_key=True
    ),
)

class Lobby(Base):
    __tablename__ = 'lobby'
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)
    owner_id = Column(Integer, ForeignKey('player.steamid'), nullable=False,
        unique=True
    )
    teams = relationship("Team", backref="lobby",
        cascade='save-update,merge,delete'
    )
    spectators = relationship("Player", secondary=spectator_table)

class Team(Base):
    __tablename__ = 'team'
    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)
    lobby_id = Column(Integer, ForeignKey('lobby.id'), nullable=False)
    players = relationship("LobbyPlayer", backref="team",
        cascade='save-update,merge,delete,delete-orphan'
    )

class LobbyPlayer(Base):
    __tablename__ = 'lobby_player'
    team_id = Column(Integer, ForeignKey('team.id'), primary_key=True)
    player_id = Column(Integer, ForeignKey('player.steamid'), primary_key=True)
    player = relationship("Player", uselist=False)
    cls = Column(Integer)

class Player(Base):
    __tablename__ = 'player'
    steamid = Column(Integer, primary_key=True)

谢谢您的帮助!

4

2 回答 2

8

我在 SA 0.7.9 上看到了同样的问题

似乎正在发生的事情是括号没有按照您想要的方式正确应用。我使用了 self_group() 来完成这项工作,但问题是你不应该使用它。这是 self_group的文档。我认为应该有比我更好的答案,但是,这应该可以让您继续前进。

query = session.query(Lobby).filter(
    ((Lobby.id == Team.lobby_id) &
    (LobbyPlayer.team_id == Team.id) &
    (LobbyPlayer.player_id == player.steamid)).self_group() |
    ((Lobby.id == spectator_table.c.lobby_id) &
    (spectator_table.c.player_id == player.steamid)).self_group()).all()

生成的 sql 如下,我相信这就是你所追求的。

SELECT lobby.id AS lobby_id, lobby.name AS lobby_name, lobby.owner_id AS lobby_owner_id 
FROM lobby, team, lobby_player, spectator 
WHERE (lobby.id = team.lobby_id AND lobby_player.team_id = team.id AND lobby_player.player_id = ?) OR (lobby.id = spectator.lobby_id AND spectator.player_id = ?)
于 2012-11-14T01:05:08.340 回答
6

托尼,我从来不知道 self_group(),最棒的!

TronPaul,您可以使用 self_group() 在周围添加括号和 ClauseElements,例如:

query = session.query(Invoice).join(CustomerQuote).reset_joinpoint()

selections = [1,2,3,4,5,6]
select_ids = Invoice.invoice_id.in_(selections).self_group()
ands = and_(CustomerQuote.customer_id==33,  
       CustomerQuote.invoice_date>=low_date,
       CustomerQuote.invoice_date<=hi_date).self_group()

query = query.filter(or_(select_ors, ands))

SELECT invoice.invoice_id AS invoice_invoice_id, [etc etc....]
FROM invoice INNER JOIN customer_quote ON 
customer_quote.customer_quote_id = invoice.customer_quote_id 
WHERE (invoice.invoice_id IN (1,2,3,4,5)) OR 
      (customer_quote.customer_id = 33 AND 
       invoice.invoice_date >= '2012-01-01' AND 
       invoice.invoice_date <= '2012-12-31')

详细说明 Tony 的回答:self_group() 对于对“二进制”表达式以外的子句进行分组很有用,它适用于任何 ClauseElement。

于 2012-11-27T18:50:14.377 回答