将复杂的原始SQL查询转换为SQLAlchemy查询API

时间:2019-01-07 12:43:42

标签: flask-sqlalchemy

有一个简单的足球数据库。 选择所有带有具体玩家的游戏,一切正常:

# By Team "A"
SELECT
    g.id
FROM "Game" g
     JOIN "Team" t
          ON g.team_a_id = t.id
     JOIN "JoinTeamPlayer" j
          ON t.id = j.team_id
              AND j.player_id = :player

UNION

# By Team "B"
SELECT
    g.id
FROM "Game" g
     JOIN "Team" t
          ON g.team_b_id = t.id
     JOIN "JoinTeamPlayer" j
          ON t.id = j.team_id
              AND j.player_id = :player

我想将其转换为SQLAlchemy查询API

我对两个外键在一张桌子上感到困惑,一个团队中可能有几个球员。我可以这样保留它,但是我敢肯定有一个很好的解决方案。


SQL表格(PostgreSQL)

CREATE TABLE "Game" (
    id        SERIAL  NOT NULL
        CONSTRAINT "Game_pkey"
            PRIMARY KEY,
    team_a_id INTEGER NOT NULL
        CONSTRAINT "Game_team_a_id_fkey"
            REFERENCES "Team",
    team_b_id INTEGER NOT NULL
        CONSTRAINT "Game_team_b_id_fkey"
            REFERENCES "Team"
);

CREATE TABLE "Team" (
    id   SERIAL  NOT NULL
        CONSTRAINT "Team_pkey"
            PRIMARY KEY,
    name VARCHAR NOT NULL
);

CREATE TABLE "Player" (
    id   SERIAL  NOT NULL
        CONSTRAINT "Player_pkey"
            PRIMARY KEY,
    name VARCHAR NOT NULL
);

CREATE TABLE "JoinTeamPlayer" (
    team_id   INTEGER NOT NULL
        CONSTRAINT "JoinTeamPlayer_team_id_fkey"
            REFERENCES "Team",
    player_id INTEGER NOT NULL
        CONSTRAINT "JoinTeamPlayer_player_id_fkey"
            REFERENCES "Player"
);

SQLAlchemy ORM映射器

class Base( db.Model ):
    __abstract__ = True
    id = Column( Integer, primary_key=True )


class Game( Base ):
    team_a_id = Column( Integer, ForeignKey( 'Team.id' ), nullable=False )
    team_a = relationship( 'Team', foreign_keys=team_a_id )

    team_b_id = Column( Integer, ForeignKey( 'Team.id' ), nullable=False )
    team_b = relationship( 'Team', foreign_keys=team_b_id )


join = Table( 'JoinTeamPlayer', Base.metadata,
              Column( 'team_id', Integer, ForeignKey( 'Team.id' ), nullable=False ),
              Column( 'player_id', Integer, ForeignKey( 'Player.id' ), nullable=False ) )


class Team( Base ):
    name = Column( String, nullable=False )
    players = relationship( 'Player', secondary=join )


class Player( Base ):
    name = Column( String, nullable=False )

0 个答案:

没有答案